Jump to content

image upload coding error"move_upload_file()"


Spraban9

Recommended Posts

 <?php
// May 25, 2010
$currentDate = date("F j, Y");

	

if($_POST['title'] != ''  && $_POST['catergory'] != ''  && $_POST['des'] != ''  && $_POST['phone'] != ''  && $_POST['add'] != ''   && $_POST['city'] != '' && $_POST['file']!='' && $_POST['date']!='' )

	
	{
		
		$title = $_POST['title'];
		$catergory = $_POST['catergory'];
		$des = $_POST['des'];
		$phone=$_POST['phone'];
		$add = $_POST['add'];                                
		$city = $_POST['city'];
		$file=$_POST['file'];
		$currentDate = $_POST['date'];
	   
		 include("include/connection.php");
		
    if (!$conn)	{
				die('Could not connect: ' . mysql_error());
			}
			
			mysql_select_db($database , $conn) or die (mysql_error());
				
					$query = mysql_query("INSERT INTO upload ads (title,catergory, des, phone ,add,city,file,date) VALUES ('".$title."','".$catergory."','".$des."', '".$phone."'	,'".$add."','".$city."','".$file."','".$currentDate."')");
			
		
			
		
		if($query){
		echo " Enter the code form your phone";
}
else {
echo "Contact number cant recieve the code";
}

mysql_close($conx);
	}




		


if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 50000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    //echo "proimage: " . $_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("proimage/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
		  
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "proimage/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "proimage/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> 
 

im new developer

i get below error

when i upload the image

 

Warning: move_uploaded_file(proimage/1.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in F:\xampp\htdocs\cleanred\uploadads.php on line 161

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'F:\xampp\tmp\php1CB1.tmp' to 'proimage/1.jpg' in F:\xampp\htdocs\cleanred\uploadads.php on line 161

Stored in: proimage/1.jpg

 

 

A couple of things to note:

 

1. You need to be sanitizing any arbitrary data from the user before inserting it into an SQL statement using mysql_real_escape_string

 

2. Get into the habit of validating both a files mime type AND extension, mime types are fairly easy to spoof. Also, creating whitelist arrays of both mime types and file extensions and then using in_array to validate is a cleaner solution.

 

3. In the call to move_uploaded_file, I recommend using an absolute path to the file vs a relative path. Most likely the error is being triggered because the directory proimage does not exist relative to the active file directory.

 

4. MYSQL has been soft deprecated and should not be used any further. Instead, use either mysqli or PDO.

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.