Image Uploader

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
enigm4_
Forum Newbie
Posts: 17
Joined: Sun Mar 12, 2006 12:40 am

Image Uploader

Post by enigm4_ »

Hey Guys,

Got an image uploader that refuses to send anything and just gives me an error. Here is the uploader code:

Code: Select all

$uploaddir='submissions';
				//set the naming scheme
				$uploadfile=$uploaddir . $_FILES['file']['name'];
				//Upload each file
				echo '<pre>';
				if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
					echo "File is valid, and was successfully uploaded.";
				} else {
					echo "Possible file upload attack!";
				}
				
				echo 'Here is some more debugging info:';
				print_r($_FILES);
				
				print "</pre>";
This is the form section that upload the file:

Code: Select all

<form enctype="multipart/form-data" action="';$_SERVER['PHP_SELF'];echo '" method="post">
						<fieldset>
							<legend>Submission</legend>
							<div class="information">
								<h4>Information</h4>
								<p>If you want to submit your work under a name other than your own, put that name in the Pseudonym field.  Otherwise we will use whatever name you used to register in our system (As shown above).</p>
							</div>
							<label for="title">Submission Title</label>
								<input name="title" id="title" type="text" class="sminp" value="'; if(isset($_POST['title'])) echo $_POST['title']; echo'" tabindex="1" /><br />
							<label for="pseudonym">Pseudonym</label>
								<input name="pseudonym" id="pseudonym" class="sminp" type="text" value="'; if(isset($_POST['pseudonym'])) echo $_POST['pseudonym']; echo'" tabindex="2" /><br />
							<label for="text">Article</label>
								<textarea cols="5" rows="8" name="text" id="text" tabindex="3">'; if(isset($_POST['text'])) echo $_POST['text']; echo'</textarea>
						</fieldset>
						<fieldset>
							<legend>File Uploads</legend>
							<div class="information">
								<h4>Information</h4>
								<p>You can only upload files that are less than 2MB in size.  If you have more than 3 items you want to upload, or your file is more than 2MB, <a href="http://condor.depaul.edu/~slytinen/instructions/zip.html">make a zip file</a> out of them.  Still too large?  <a href="mailto:online@reakt.com.au">Email them</a> to us.</p>
							</div>
							<label for="file1">File 1</label>
								<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
								<input name="file1" id="file1" class="inputbox" type="file" tabindex="4" /><br />
							<label for="file2">File 2</label>
								<input name="file2" id="file2" class="inputbox" type="file" tabindex="5" /><br />
							<label for="file3">File 3</label>
								<input name="file3" id="file3" class="inputbox" type="file" tabindex="6" /><br />
						</fieldset>
						<label></label><input name="submit" type="submit" value="Submit" class="sminp" tabindex="7" />
					</form>
And this is the error that the uploader gives me

Code: Select all

Possible file upload attack!Here is some more debugging info:Array
(
    [file1] => Array
        (
            [name] => 10223-b.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpxQHgfT
            [error] => 0
            [size] => 58382
        )

    [file2] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [file3] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

)
Does anyone have anyu idea why it would be doing this? And where I can go to make it work. Also, if anyone knows of a good site for shecking the format that'd be great too ^_^

Cheers
~Chris
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hint: echo $uploadfile
enigm4_
Forum Newbie
Posts: 17
Joined: Sun Mar 12, 2006 12:40 am

Post by enigm4_ »

Thanks Feyd.

It outputs the right thing now, but it still gives me an error instead of uploading the file.

Are there other errors?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's the new code?

remember to use basename() on the "name" given in the file array, as some browsers send full path :roll:
kkashi
Forum Newbie
Posts: 8
Joined: Mon Feb 27, 2006 10:40 am

Post by kkashi »

Here is a nice file uploader. It could be updated to do batch uploads.

http://www.air4web.com/files/upload/
enigm4_
Forum Newbie
Posts: 17
Joined: Sun Mar 12, 2006 12:40 am

Post by enigm4_ »

Kkashi - Thanks for that, but not really what I'm after for this project.

Feyd:

new Upload code is :

Code: Select all

//Set the directory that the files will be moved to
				$uploaddir='submissions/';
				//set the naming scheme
				$uploadfile=$uploaddir . basename($_FILES['file1']['name']);
				//Upload each file
				echo '<pre>';
				if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
					echo "File is valid, and was successfully uploaded.";
				} else {
					echo "Possible file upload attack!";
				}
				
				echo 'Here is some more debugging info:';
				print_r($_FILES);
				
				print "</pre>";
Form is the same as before
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hint: look closely at the index names in the $_FILES array compared to your code thus far.
enigm4_
Forum Newbie
Posts: 17
Joined: Sun Mar 12, 2006 12:40 am

Post by enigm4_ »

Thanking you verily ^_^

I've been staring at all my code so long I can't even see what is right in front of my nose :P
Post Reply