Jump to content

PHP Image upload help: copy() [function.copy]: Filename cannot be empty in


melting_dog

Recommended Posts

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

$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

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!

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.

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.

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!

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.

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.