contra10 Posted January 4, 2009 Share Posted January 4, 2009 hey im trying to make this code work for an image upload <?php /*configuration */ $root= "C:\\wamp\\www\\imagealbum\\"; $urlroot = "http://localhost/images/"; $max_width= 420; $max_height = 600; $overwrite_images = false; $target_dirs = array("article", "banners"); if(!function_exists(getimagesize)){ die("getimagesize() required."); } $location = strval($_POST['location']); $newname = strval($_POST['newname']); $upfile = $_FILES['upfile']['tmp_name']; $upfile_name = $_FILES['upfile']['name']; if($newname){ $newname= preg_replace('/[^A-Za-z0-9_.-]/', '', $newname); }else{ $newname = preg_replace('/[^A-Za-z0-9_.-]/', '',$upfile_name); } if (!in_array($location, $target_dirs)){ die("invalid target directory."); }else{ $urlroot .="/location"; } if (!upfile){ die("no file for upload. "); } $file_types = array( "image/jpeg" => "jpg", "image/pjpeg" =>"jpg", "image/gif" => "gif", "image/png" => "png", ); $width = null; $height = null; $img_info = getimagesize($upfile); $upfile_type = $img_info["mime"]; list ($width, $height, $t, $attr) = $img_info; if (!$file_types[$upfile_type]){ die("Image must be in JPEG, GIF, or PNG format"); }else{ $file_suffix = $file_types[$upfile_type]; } if($width > $max_width || $height > $max_height){ die ("size $width x $height exceeds maximum $max_width x $max_height."); } $newname = preg_replace('/\.(jpe?g|gif|png)$/i', ""); $newname .= $file_suffix; $new_fullpath = "$root/$location/$newname"; if((!overwrite_images) && file_exists($new_fullpath)){ die("file exists; will not overwrite."); } if(!copy($upfile,$new_fullpath)){ die("copy failed."); } $image_url = "$urlroot/$newname"; print "HTML for image:</strong><br><textarea cols=\"80\" rows\"4\">"; print "<img src\"image_url\" $attr alt=\"$upfile_name\" border=\"0\"/>"; print "</textarea><br>"; print '<a href="http://localhost/photoupload/"> Upload another image></a>'; ?> instead i get these errors Warning: Wrong parameter count for preg_replace() in C:\wamp\www\image_process.php on line 62 Warning: copy(file:///C|/wamp/www/images//article/jpg) [function.copy]: failed to open stream: Invalid argument in C:\wamp\www\image_process.php on line 70 copy failed. Link to comment https://forums.phpfreaks.com/topic/139400-cant-get-image-to-upload/ Share on other sites More sharing options...
darkfreaks Posted January 4, 2009 Share Posted January 4, 2009 <?php $newname = preg_replace('/\.(jpe?g|gif|png)$/i', "");?> Change to: <?php $newname = preg_replace('/\.(jpe?g|gif|png)$/i', "",$newname);?> Link to comment https://forums.phpfreaks.com/topic/139400-cant-get-image-to-upload/#findComment-729167 Share on other sites More sharing options...
contra10 Posted January 4, 2009 Author Share Posted January 4, 2009 thxs that solved the first part but i still get this error Warning: copy(C:\wamp\www\imagealbum\/article/n28289389880_7562jpg) [function.copy]: failed to open stream: No such file or directory in C:\wamp\www\image_process.php on line 70 copy failed. the directory or file does exists Link to comment https://forums.phpfreaks.com/topic/139400-cant-get-image-to-upload/#findComment-729169 Share on other sites More sharing options...
contra10 Posted January 4, 2009 Author Share Posted January 4, 2009 is my file not being found? could it be the file path? Link to comment https://forums.phpfreaks.com/topic/139400-cant-get-image-to-upload/#findComment-729176 Share on other sites More sharing options...
darkfreaks Posted January 4, 2009 Share Posted January 4, 2009 copy() is not for images but rather for text files try this: http://us3.php.net/manual/en/function.move-uploaded-file.php Link to comment https://forums.phpfreaks.com/topic/139400-cant-get-image-to-upload/#findComment-729177 Share on other sites More sharing options...
contra10 Posted January 4, 2009 Author Share Posted January 4, 2009 if(!move_uploaded_file ($upfile,$new_fullpath)){ die("copy failed."); for some reason it still doesnt' work i get Warning: move_uploaded_file(C:\wamp\www\imagealbum\/article/world2png) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\wamp\www\image_process.php on line 70 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\phpCA.tmp' to 'C:\wamp\www\imagealbum\/article/world2png' in C:\wamp\www\image_process.php on line 70 copy failed. } Link to comment https://forums.phpfreaks.com/topic/139400-cant-get-image-to-upload/#findComment-729180 Share on other sites More sharing options...
PFMaBiSmAd Posted January 4, 2009 Share Posted January 4, 2009 Images are files. Did anyone read the error message - C:\wamp\www\imagealbum\/article/n28289389880_7562jpg You have got too many slashes. Link to comment https://forums.phpfreaks.com/topic/139400-cant-get-image-to-upload/#findComment-729181 Share on other sites More sharing options...
darkfreaks Posted January 4, 2009 Share Posted January 4, 2009 haha i just noticed that right before you posted Link to comment https://forums.phpfreaks.com/topic/139400-cant-get-image-to-upload/#findComment-729182 Share on other sites More sharing options...
contra10 Posted January 4, 2009 Author Share Posted January 4, 2009 hould i get rid of both slashes, back/front, i guess my question is how do i make them connect Link to comment https://forums.phpfreaks.com/topic/139400-cant-get-image-to-upload/#findComment-729183 Share on other sites More sharing options...
bubbasheeko Posted January 4, 2009 Share Posted January 4, 2009 I noticed the slashes too. Another thing that bothers me about the filename and path: C:\wamp\www\imagealbum\/article/world2png Notice there is no '.' between the file name and file extension. The problem is your $root variable contents. You have a preg_replace that wants to use the '/' slash. However, in Windows it uses the '\'. To make it accessible on the website we need to use the '/'. Process of elimination is to fix the $root variable contents. So try this for $root: $root="C:/wamp/www/imagealbum"; In WAMP the httpd.conf utilizes the '/' in the DocumentRoot. To fix the '.' missing between the file name and the extension....somebody please recheck the preg_replace :S Link to comment https://forums.phpfreaks.com/topic/139400-cant-get-image-to-upload/#findComment-729184 Share on other sites More sharing options...
darkfreaks Posted January 4, 2009 Share Posted January 4, 2009 he needs to remove \/ and the . Link to comment https://forums.phpfreaks.com/topic/139400-cant-get-image-to-upload/#findComment-729186 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.