NaveAdair Posted April 24, 2009 Share Posted April 24, 2009 I'm in the process of making an ACP, which include image uploading. Since it's been a long time since I did something like this in PHP (I'm mostly a RoR programmer now >_>) I followed a tutorial. Index, with form (under "Add a Project"): http://rfcy.net/residential/beta/index.txt Page the form links to: http://rfcy.net/residential/beta/add.txt I'm sure the problem is obvious, hopefully easy to fix. Basically, the data goes into the MySQL database, but the image URL doesn't... it remains blank. Thanks in advance for any help. Quote Link to comment https://forums.phpfreaks.com/topic/155547-solved-image-upload-not-working/ Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 you have an extra curly bracket. change this: function upload($image) { $image=$_FILES['image']['name']; if ($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension == "jpg") || ($extension == "jpeg") || ($extension == "png") || ($extension == "gif")) { $size=filesize($_FILES['image']['tmp_name']); $image_name=time().'.'.$extension; $newname="images/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); } } return $newname; } to this function upload($image) { $image=$_FILES['image']['name']; if ($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension == "jpg") || ($extension == "jpeg") || ($extension == "png") || ($extension == "gif")) { $size=filesize($_FILES['image']['tmp_name']); $image_name=time().'.'.$extension; $newname="images/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); } return $newname; } Quote Link to comment https://forums.phpfreaks.com/topic/155547-solved-image-upload-not-working/#findComment-818576 Share on other sites More sharing options...
Bauer418 Posted April 24, 2009 Share Posted April 24, 2009 Actually...you just took out a bracket that he needed, his code won't parse now. @NaveAdair: I'm not sure exactly what your code is supposed to do. To upload files, you need to be using the $_FILES array, not the $_POST array. Why are you passing $_POST['pic1'] to upload()? Quote Link to comment https://forums.phpfreaks.com/topic/155547-solved-image-upload-not-working/#findComment-818601 Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 Actually...you just took out a bracket that he needed, his code won't parse now. @NaveAdair: I'm not sure exactly what your code is supposed to do. To upload files, you need to be using the $_FILES array, not the $_POST array. Why are you passing $_POST['pic1'] to upload()? oops your right... missed the last bracket Quote Link to comment https://forums.phpfreaks.com/topic/155547-solved-image-upload-not-working/#findComment-818603 Share on other sites More sharing options...
NaveAdair Posted April 24, 2009 Author Share Posted April 24, 2009 Actually...you just took out a bracket that he needed, his code won't parse now. @NaveAdair: I'm not sure exactly what your code is supposed to do. To upload files, you need to be using the $_FILES array, not the $_POST array. Why are you passing $_POST['pic1'] to upload()? I'm not sure myself. >_> Might that be the solution to my problem? EDIT: Ugh, I might need to find a better tutorial, since the one I went off just threw code at me... EDIT 2: Tried this function upload($image) { if ($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension == "jpg") || ($extension == "jpeg") || ($extension == "png") || ($extension == "gif")) { $size=filesize($_FILES['image']['tmp_name']); $image_name=time().'.'.$extension; $newname="images/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); } return $newname; } } $pic1 = upload($_FILES['pic1']['name']); $pic2 = upload($_FILES['pic2']['name']); $pic3 = upload($_FILES['pic3']['name']); $pic4 = upload($_FILES['pic4']['name']); $pic5 = upload($_FILES['pic5']['name']); $pic6 = upload($_FILES['pic6']['name']); $pic7 = upload($_FILES['pic7']['name']); $pic8 = upload($_FILES['pic8']['name']); $pic9 = upload($_FILES['pic9']['name']); $pic10 = upload($_FILES['pic10']['name']); $pic11 = upload($_FILES['pic11']['name']); $pic12 = upload($_FILES['pic12']['name']); Didn't work. Worth a try, I guess. >_> Quote Link to comment https://forums.phpfreaks.com/topic/155547-solved-image-upload-not-working/#findComment-818604 Share on other sites More sharing options...
NaveAdair Posted April 24, 2009 Author Share Posted April 24, 2009 Also tried: function upload($image) { if ($_FILES[$image]['name']) { $filename = stripslashes($_FILES[$image]['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension == "jpg") || ($extension == "jpeg") || ($extension == "png") || ($extension == "gif")) { $image_name=time().'.'.$extension; $newname="images/".$image_name; $copied = copy($_FILES[$image]['tmp_name'], $newname); } return $newname; } } $pic1 = upload("pic1"); $pic2 = upload("pic2"); $pic3 = upload("pic3"); $pic4 = upload("pic4"); $pic5 = upload("pic5"); $pic6 = upload("pic6"); $pic7 = upload("pic7"); $pic8 = upload("pic8"); $pic9 = upload("pic9"); $pic10 = upload("pic10"); $pic11 = upload("pic11"); $pic12 = upload("pic12"); Now I get a URL in the database, but the image doesn't actually copy over, and the error: Warning: copy(images/1240605845.jpg) [function.copy]: failed to open stream: No such file or directory in C:\xampp\htdocs\Residential\admin\projects\add.php on line 25 Quote Link to comment https://forums.phpfreaks.com/topic/155547-solved-image-upload-not-working/#findComment-818614 Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 instead of using the copy function, try using the move_uploaded_file function so change the following $copied = copy($_FILES[$image]['tmp_name'], $newname); to $copied = move_uploaded_file($_FILES[$image]['tmp_name'], $newname); hope that helped Quote Link to comment https://forums.phpfreaks.com/topic/155547-solved-image-upload-not-working/#findComment-818618 Share on other sites More sharing options...
NaveAdair Posted April 24, 2009 Author Share Posted April 24, 2009 That doubled the amount of error messages, for better or worse. Warning: move_uploaded_file(images/1240606194.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\xampp\htdocs\Residential\admin\projects\add.php on line 25 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php327.tmp' to 'images/1240606194.jpg' in C:\xampp\htdocs\Residential\admin\projects\add.php on line 25 Quote Link to comment https://forums.phpfreaks.com/topic/155547-solved-image-upload-not-working/#findComment-818621 Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 two things. Make sure the directory you are ttrying to move the files to exists. something like: ff (!file_exists('images/')) { mkdir('images'); } this will check if the dir exists, and if not it will make it. now make sure that your script has access to moving, and creating folders/files in your server. It has to have its permissions set to 777 i believe. Quote Link to comment https://forums.phpfreaks.com/topic/155547-solved-image-upload-not-working/#findComment-818625 Share on other sites More sharing options...
NaveAdair Posted April 24, 2009 Author Share Posted April 24, 2009 Well, that fixed it. I forgot to create the "images" directory. XD Thanks, all. Quote Link to comment https://forums.phpfreaks.com/topic/155547-solved-image-upload-not-working/#findComment-818631 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.