roxki Posted February 20, 2008 Share Posted February 20, 2008 Hello dear PHP users. I'm currently making an Gallery for a friend, or atleast I was, until I ran into a pretty annoying problem. He wanted to be able to upload multiple files at once, so I made an HTML form like this (just an example): <form action="" enctype="multipart/form-data" method="post"> File1: <input type="file" name="picture[]"><br> File2: <input type="file" name="picture[]"><br> File3: <input type="file" name="picture[]"><br> <input type="submit" value="Upload"> </form> At the PHP I'm running the array throuh a while where I do some file-type checks and file-size checks before starting to upload, which I've no trouble at all with. But then my script returns my error message because my move_uploaded_file() function won't move the uploaded file. So first I check $_FILES["picture"]["error"] for any errors, but it returns a '0'. Second I check the CHMOD on my specified destination directory which got 7,7,7 permission. So shouldn't be a problem either. Third I do a file_exists() on the destination directory which returns '1', so there's nothing wrong with that directory either. So at the moment I'm pretty lost, I've made many other uploading systems, well which only upload one file at once. The script doesn't come with any warnings, just the message I told it to say IF move_uploaded_file() went wrong. Have I forgot something important? I've been sitting with this for several hours now without any luck at all. Hope you can help me out, 'cause this is freaking me out! Quote Link to comment Share on other sites More sharing options...
Chris92 Posted February 20, 2008 Share Posted February 20, 2008 Maybe because 'picture' is an array you need to add extra []'s: $_FILES['picture'][]['type'] .etc Quote Link to comment Share on other sites More sharing options...
roxki Posted February 20, 2008 Author Share Posted February 20, 2008 Some codes for those who should be interested in that: $upload_dir = "images/galleri/".$data_galleri->navn; $thumb_dir = "images/galleri/thumb/".$data_galleri->navn; foreach($_FILES["pic"]["error"] as $i => $fil) { if(check_file("pic", $_FILES["pic"]["name"][$i], $_FILES["pic"]["type"][$i], $_FILES["pic"]["size"][$i]) == true) { $sql_do = "insert into galleri_pic(navn) values('$data->navn')"; if(mysql_query($sql_do)) { $used_id = mysql_insert_id(); $tmp_file = $_FILES["pic"]["tmp_name"][$i]; $upload_img = $upload_dir."/".$used_id.".jpg"; if(move_uploaded_file($tmp_file, $upload_img)) { if(make_thumb($upload_img, $data_settings->thumb_h, $data_settings->thumb_w, $thumb_dir, $used_it, $data_settings->quality)) { echo "Picture has been uploaded"; } else { if(mysql_query("delete from galleri_pic where id = '$used_id'")) { echo "Couldn't upload the picture<br>"; } } } else { if(mysql_query("delete from galleri_pic where id = '$used_id'")) { echo "An error occured during uploading of the file<br>"; } } } } else { echo "The picture doesn't fit the requirements.<br>"; } } Quote Link to comment Share on other sites More sharing options...
roxki Posted February 20, 2008 Author Share Posted February 20, 2008 Okay forgot that in the code I've 2 selfmade functions, don't look at them, they work fine.. And just to get back to the topic, as far as I can see it's the move_uploaded_file() part which doesn't work as it should. if(move_uploaded_file($tmp_file, $upload_img)) { if(make_thumb()) { echo "Picture has been uploaded"; } } else { echo "This is the error message I keep getting!"; } Quote Link to comment Share on other sites More sharing options...
Chris92 Posted February 20, 2008 Share Posted February 20, 2008 I think you need to put $i++ somewhere in there, otherwise it's going to keep trying to upload $_FILES["pic"]["tmp_name"][0]; Quote Link to comment Share on other sites More sharing options...
roxki Posted February 20, 2008 Author Share Posted February 20, 2008 I think you need to put $i++ somewhere in there, otherwise it's going to keep trying to upload $_FILES["pic"]["tmp_name"][0]; I've tried make the script output the 2 variables used in the move_uploaded_file() function, $tmp_file and $upload_img. They both show the data from its own array. Not the same data as it would've been if it worked as you mentioned. But I'll try it out anyways. At the moment I'm open for any help! Quote Link to comment Share on other sites More sharing options...
roxki Posted February 20, 2008 Author Share Posted February 20, 2008 Nothing changed when tried to change $i with $i++.. Any other suggestions? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 20, 2008 Share Posted February 20, 2008 Your $i's are fine. If move_uploaded_file is returning false, and no error is being outputted, then you don't have error reporting turned on. Turn that on and let us know what errors you get. Also, try adding this code right before the move_uploaded_file and see what happens: if(!is_dir($upload_dir)) die("Upload dir doesn't exist: {$upload_dir}"); if(!is_writable($upload_dir)) die("Upload dir is not writable: {$upload_dir}"); echo "TMP FILE: {$tmp_file}\n"; echo "NEW FILE: {$upload_img}\n"; Quote Link to comment Share on other sites More sharing options...
roxki Posted February 20, 2008 Author Share Posted February 20, 2008 Could be something about error reporting aren't turned on. And I'm not sure if I've permission to it, since I'm not hosting this site myself.. And about the dir and access.. well, as I wrote above then I've done file_exists($upload_dir) AND checked that I've full permission to the directories (CHMOD 777). Could you please tell me the exact command to turn on the error reporting? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 20, 2008 Share Posted February 20, 2008 Put this at the beginning of your script: ini_set('display_errors', 1); error_reporting (E_ALL); Quote Link to comment Share on other sites More sharing options...
roxki Posted February 20, 2008 Author Share Posted February 20, 2008 Okay just did a check on is_dir($upload_dir) and one on is_writeable($upload_dir) which both returned '1' (true). On this try the: - TMP FILE: /tmp/phpbSwaEJ ($tmp_file) - NEW FILE: images/galleri/test/115.jpg ($upload_img) Nothing surprised here.. But the error reporting did surprise me, alot!: Notice: Undefined variable: data in /hsphere/local/home/roxki/test.krelle.dk/v6/admin/galleri.php on line 288 Warning: move_uploaded_file(): SAFE MODE Restriction in effect. The script whose uid/gid is 176418/176418 is not allowed to access /hsphere/local/home/roxki/test.krelle.dk/v6/images/galleri/test owned by uid/gid 398/398 in /hsphere/local/home/roxki/test.krelle.dk/v6/admin/galleri.php on line 295 So it says that it's not allowed access to the directory, how can that be? I've given every single folder, from the root -> the specified directory full permission. AND I've upload systems like this on this webserver which work without any problem at all... Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 20, 2008 Share Posted February 20, 2008 Yes, but in safe mode, the directory needs to be owned by the process running PHP. Read more about it here: http://us3.php.net/features.safe-mode Quote Link to comment Share on other sites More sharing options...
roxki Posted February 20, 2008 Author Share Posted February 20, 2008 Okay that confused me alot.. So I read your link a couple of times and still don't understand much of that.. But I went to my webhoster's support site where I could see that I won't be able to turn Safe mode Off, if it was that you were thinking about. Second I also saw that there shouldn't be any problems at all with file uploading aslong I just write the directory-path in the right way (relative). So I was thinking maybe you've an solution how to be the right owner to avoid this error? Quote Link to comment Share on other sites More sharing options...
roxki Posted February 20, 2008 Author Share Posted February 20, 2008 Okay problem got solved now. It was the PHP Safe Mode which doesn't allow me to upload stuff from PHP into a directory created by PHP.. I'd like to thank those who've been helping with solving this problem! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.