Page 1 of 1

Image Uploader

Posted: Fri Mar 17, 2006 7:14 am
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

Posted: Fri Mar 17, 2006 8:08 am
by feyd
hint: echo $uploadfile

Posted: Fri Mar 17, 2006 8:15 am
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?

Posted: Fri Mar 17, 2006 8:22 am
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:

Posted: Fri Mar 17, 2006 8:36 am
by kkashi
Here is a nice file uploader. It could be updated to do batch uploads.

http://www.air4web.com/files/upload/

Posted: Fri Mar 17, 2006 9:27 am
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

Posted: Fri Mar 17, 2006 10:02 am
by feyd
hint: look closely at the index names in the $_FILES array compared to your code thus far.

Posted: Fri Mar 17, 2006 10:10 am
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