Forum Moderators: coopster

Message Too Old, No Replies

Upload error!

         

AlexLee

9:02 pm on Feb 2, 2005 (gmt 0)

10+ Year Member



Hi, I am using this code to upload my images.

<?php
$file_realname = trim($_FILES['userfile']['name']);
$uploaddir = "images/";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $file_realname))
{
echo $file_realname." has been uploaded";
}
else
{
echo "<strong>$file_realname</strong> did not upload!";
}
?>

And I get this kind of error when I tries to upload a big file.

Notice: Undefined index: userfile in C:\Program Files\Apache Group\Apache2\htdocs\FYP\upload\upload.php on line 2

Notice: Undefined index: userfile in C:\Program Files\Apache Group\Apache2\htdocs\FYP\upload\upload.php on line 5
did not upload!

Is there a way to expend the limit of the file size? And restrict user to upload all know pictures type only.

rlkanter

9:27 pm on Feb 2, 2005 (gmt 0)

10+ Year Member



The max filesize is in your php.ini file, upload_max_filesize

[us3.php.net...] looks like it had some more info on restriction the file upload, but I've never tried it.

adamnichols45

9:33 pm on Feb 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i have the same problem but i want to allow the user to go back and select another image how can i do this please - at the moment the page is just expired when i click the broswers back button

dreamcatcher

11:32 pm on Feb 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And restrict user to upload all know pictures type only.

You could store all the extenstions you want to allow in an array and then check against that using the in_array function when you process your data. Or you could check against mime types.

AlexLee

12:52 am on Feb 3, 2005 (gmt 0)

10+ Year Member



Like if they clicked browse, the window will only display pictures file. Not All Files.

coopster

1:49 am on Feb 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There is nothing you can do there, the files listed in the "Browse" window are controlled by the client OS.

AlexLee

2:10 am on Feb 3, 2005 (gmt 0)

10+ Year Member



Then how about the error check for file size.
If file size is too big. echo "File size too big. Please select one that is smaller than 300kb.";

coopster

2:38 am on Feb 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, you can do quite a bit of error checking. See message number two in this similar thread regarding file uploads [webmasterworld.com].

AlexLee

3:30 pm on Feb 3, 2005 (gmt 0)

10+ Year Member



Manged to read up a little more on file upload today and played around with my codes.

I changed my php.ini to allow a max upload of 1MB. And I did so in my form too.
<input type="hidden" name="MAX_FILE_SIZE" value="1048576">

Yes the browser does catch the error now. Displaying error code 2. Which means "Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form."

This error works all the way until 10MB only. After 10MB...

Notice: Undefined index: userfile in C:\Program Files\Apache Group\Apache2\htdocs\FYP\admin\upload.php on line 2

Notice: Undefined index: userfile in C:\Program Files\Apache Group\Apache2\htdocs\FYP\admin\upload.php on line 5
did not upload!
Notice: Undefined index: userfile in C:\Program Files\Apache Group\Apache2\htdocs\FYP\admin\upload.php on line 13

And I had wanted to rename my files afer they are uploaded. Figured that $_FILES['userfile']['name'] gets the full name together with the extension. So when I change my file name. It is without extension. How do I change it so that only the name changes but the extension remains?

ergophobe

4:37 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I suppose that the upload is failing utterly at that point, so the $_FILES['userfile'] array isn't set at all. If you

print_r($_FILES);

what do you get? I'm assuming you need to check first, access upon success.

if(!empty($_FILES['userfile']))
{
Do stuff with my upload
}
else
{
send error info
}