Jump to content

Last Error With Image Upload


justlukeyou

Recommended Posts

Hi,

 

I am trying to introduce an error upload script however I have one last error which I am struggling to fix.

 

This is the error message. I had 3 other errors which I have managed to fix. I have even put in the full link however this doesn't seem to fix the problem. Any suggestions please?

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php5BdCin' to 'http://www.website.com/test/imagetest.gif' in /home/website.com/test/upload_file.php on line 79

 

 

<?php
if ($_FILES["file"]["error"] > 0)
 {
 echo "Error: " . $_FILES["file"]["error"] . "<br />";
 }
else
 {
 echo "Upload: " . $_FILES["file"]["name"] . "<br />";
 echo "Type: " . $_FILES["file"]["type"] . "<br />";
 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
 echo "Stored in: " . $_FILES["file"]["tmp_name"];
 }
?>
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
 {
 if ($_FILES["file"]["error"] > 0)
   {
   echo "Error: " . $_FILES["file"]["error"] . "<br />";
   }
 else
   {
   echo "Upload: " . $_FILES["file"]["name"] . "<br />";
   echo "Type: " . $_FILES["file"]["type"] . "<br />";
   echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
   echo "Stored in: " . $_FILES["file"]["tmp_name"];
   }
 }
else
 {
 echo "Invalid file";
 }
?>
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
 {
 if ($_FILES["file"]["error"] > 0)
   {
   echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
   }
 else
   {
   echo "Upload: " . $_FILES["file"]["name"] . "<br />";
   echo "Type: " . $_FILES["file"]["type"] . "<br />";
   echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
   echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
   if (file_exists("http://www.website.com/test/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
   else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "http://www.website.com/test/" . $_FILES["file"]["name"]);				   *******ERROR RELATED HERE*******
  echo "Stored in: " . "http://www.website.com/test/" . $_FILES["file"]["name"];
  }
   }
 }
else
 {
 echo "Invalid file";
 }
?>

Link to comment
Share on other sites


     move_uploaded_file($_FILES["file"]["tmp_name"],
     "http://www.website.com/test/" . $_FILES["file"]["name"]);                   *******ERROR RELATED HERE*******

 

You don't move_uploaded_file to your website URL; you move it to a filesytem directory. In this case, you might want to use $_SERVER['DOCUMENT_ROOT'] as the path.

 

By the way, why do you have the same code repeated over and over?

Link to comment
Share on other sites

You can't copy files into a website. You have to copy them as the actual files they are. Figure out where the /test path is on your filesystem (spoiler: $_SERVER["DOCUMENT_ROOT"] . "/test") and copy the files into that.

 

And you know you've got a block of code there repeated, right?

And don't use the ["type"] to tell the file type - it cannot be trusted and might even be wrong in some (coughIE) browsers. Look at the extension first and, for images, use getimagesize to be extra sure.

Link to comment
Share on other sites

Cheers dudes, my silly mistake.

 

I was using this guide http://php.about.com/od/advancedphp/ss/php_file_upload_3.htm

 

I have one last issue which does make sense. I have sent the destination as "/home/domain/test" so the image is entered into a test folder. However the image is being entered into "/home/domain/" the folder above the test folder. Can anyone advise how to place it into the actual test folder?

 

    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    if (file_exists("/home/domain/test" . $_FILES["file"]["name"]))
	  {
	  echo $_FILES["file"]["name"] . " already exists. ";
	  }
    else
	  {
	  move_uploaded_file($_FILES["file"]["tmp_name"],
	  "/home/domain/test" . $_FILES["file"]["name"]);						   
	  echo "Stored in: " . "/home/domain/test" . $_FILES["file"]["name"];
	  }
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.