melting_dog Posted March 12, 2012 Share Posted March 12, 2012 Hi guys, I am wirting a script to upload an image file, but I get the error: copy() [function.copy]: Filename cannot be empty in Obviouly it has something to do with the copy() function - (copy($HTTP_POST_FILES['product_img']['tmp_name'], $path)) I think it is not copying from my temp folder or something. Can anyone help me out? Code is: //IMAGE FILE $file_name = $_FILES['product_img']['name']; echo '<p>File Name:' . $file_name . '</p>'; //IMAGE UPLOAD // random 4 digit to add to file name $random_digit=rand(0000,9999); //combine random digit to file name to create new file name //use dot (.) to combile these two variables $new_image_name = $random_digit.$file_name; echo $new_image_name; //SET DESINATION FOLDER $path= "uploads/".$new_image_name; echo '<p>Path:' . $path . '</p>'; if($product_img !=none) { if(copy($HTTP_POST_FILES['product_img']['tmp_name'], $path)) { echo "Successful<BR/>"; echo "File Name :".$new_file_name."<BR/>"; echo "File Size :".$HTTP_POST_FILES['product_img']['size']."<BR/>"; echo "File Type :".$HTTP_POST_FILES['product_img']['type']."<BR/>"; } else { echo "Image upload Error<BR/>"; } } Any help would be greatly appreciated! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/258731-php-image-upload-help-copy-functioncopy-filename-cannot-be-empty-in/ Share on other sites More sharing options...
AyKay47 Posted March 12, 2012 Share Posted March 12, 2012 $HTTP_POST_FILES is deprecated and should no longer be used. Use the superglobal $_FILES array instead. You should be checking for the files existence before attempting to copy it. Also, use move_uploaded_file Quote Link to comment https://forums.phpfreaks.com/topic/258731-php-image-upload-help-copy-functioncopy-filename-cannot-be-empty-in/#findComment-1326377 Share on other sites More sharing options...
melting_dog Posted March 14, 2012 Author Share Posted March 14, 2012 Thanks for that. I have updated both those: //Form data sent $product_name = $_POST['product_name']; $product_active = $_POST['product_active']; $product_cat = $_POST['product_cat']; $product_desc = $_POST['product_desc']; $prod_inoutdoor = $_POST['prod_inoutdoor']; $prod_stackable = $_POST['prod_stackable']; $prod_discounted = $_POST['prod_discounted']; //IMAGE FILE $file_name = $_FILES['product_img']['name']; echo '<p>File Name:' . $file_name . '</p>'; //IMAGE UPLOAD // random 4 digit to add to file name $random_digit=rand(0000,9999); //combine random digit to you file name to create new file name //use dot (.) to combile these two variables $new_image_name = $random_digit.$file_name; echo $new_image_name; //SET DESINATION FOLDER $path= "http://localhost/website/uploads/".$new_image_name; echo '<p>Path:' . $path . '</p>'; if($product_img !=none) { if(move_uploaded_file($_FILES['product_img']['tmp_name'], $path)) { echo "Successful<BR/>"; echo "File Name :".$new_file_name."<BR/>"; echo "File Size :".$HTTP_POST_FILES['product_img']['size']."<BR/>"; echo "File Type :".$HTTP_POST_FILES['product_img']['type']."<BR/>"; } else { echo "Image upload Error<BR/>"; } However, I now get this error: move_uploaded_file(http://localhost/website/uploads/5965thailand.jpg) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections in I know this may have something to do with permissions. All permissions on that folder are set to read/write. This is obviously also running off my localhost. Can anyone give me an idea of why this is happening? Thanks heaps again! Quote Link to comment https://forums.phpfreaks.com/topic/258731-php-image-upload-help-copy-functioncopy-filename-cannot-be-empty-in/#findComment-1327231 Share on other sites More sharing options...
PFMaBiSmAd Posted March 14, 2012 Share Posted March 14, 2012 You are trying to move/copy a file within the file system on the server and must use valid file system path. A URL is not a file system path. It is a method of requesting a document through a web server and as the error messages states - HTTP wrapper does not support writeable connections. You must use either an absolute or relative file system path/filename as the destination. Quote Link to comment https://forums.phpfreaks.com/topic/258731-php-image-upload-help-copy-functioncopy-filename-cannot-be-empty-in/#findComment-1327238 Share on other sites More sharing options...
AyKay47 Posted March 14, 2012 Share Posted March 14, 2012 There was no reason to change $path from the original post. Quote Link to comment https://forums.phpfreaks.com/topic/258731-php-image-upload-help-copy-functioncopy-filename-cannot-be-empty-in/#findComment-1327240 Share on other sites More sharing options...
cpd Posted March 14, 2012 Share Posted March 14, 2012 You are trying to move/copy a file within the file system on the server and must use valid file system path. A URL is not a file system path. It is a method of requesting a document through a web server and as the error messages states - HTTP wrapper does not support writeable connections. You must use either an absolute or relative file system path/filename as the destination. @ melting_dog In other words you need something like "C:/path/to/the/desired/folder/". Or on a server its usually something like "/home/username/public_html/desired/folder/" dependent on permissions and where you want to store the file etc. Quote Link to comment https://forums.phpfreaks.com/topic/258731-php-image-upload-help-copy-functioncopy-filename-cannot-be-empty-in/#findComment-1327241 Share on other sites More sharing options...
melting_dog Posted March 19, 2012 Author Share Posted March 19, 2012 Hi all, Thanks for the responses. I had tried all that but now have found the issue. I should have mentioned first off - I am making a Wordpress Plugin and thus I have since found out WP needs to use certain 'tags'. So for WP plugins this is the correct path: $path = WP_PLUGIN_DIR."/prodadd/uploads/".$new_image_name; OR to get to the Wordpress Sites root: $path = ABSPATH."/uploads/".$new_image_name; Thanks all! Quote Link to comment https://forums.phpfreaks.com/topic/258731-php-image-upload-help-copy-functioncopy-filename-cannot-be-empty-in/#findComment-1328880 Share on other sites More sharing options...
Jamied_uk Posted March 19, 2012 Share Posted March 19, 2012 im looking for database help to update mysql database with a url supplyed by a logged in user using where id = $id and to have the url stored in database any ideas on creating this small form that will do the job, im stuck for a while tryingto find help but theres not alot about. Quote Link to comment https://forums.phpfreaks.com/topic/258731-php-image-upload-help-copy-functioncopy-filename-cannot-be-empty-in/#findComment-1329012 Share on other sites More sharing options...
AyKay47 Posted March 19, 2012 Share Posted March 19, 2012 im looking for database help to update mysql database with a url supplyed by a logged in user using where id = $id and to have the url stored in database any ideas on creating this small form that will do the job, im stuck for a while tryingto find help but theres not alot about. Start a new thread in the proper forum with your question. Quote Link to comment https://forums.phpfreaks.com/topic/258731-php-image-upload-help-copy-functioncopy-filename-cannot-be-empty-in/#findComment-1329015 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.