Page 1 of 1

upload new file but can't rename..???

Posted: Tue Jun 12, 2007 3:49 am
by aris1234
Hi...

I want update (remane) old file name in DB with new file name.

its my code only upload new file but can't update(rename) new file name :
please help..???

Code: Select all

<?php
 
//&#1057;heck that we have a file
$folder = "../property/$spid";
if(!file_exists($folder))
{
   mkdir($folder);
}
$sql = "UPDATE `$table` SET str_image1='$simage1' WHERE int_prop_ID='$spid'";
}
			if(mysql_query($sql, $conn) == false)
				  {
					exit(mysql_error());
				  }
			  else
				{
					$property_id = mysql_insert_id();
				}

		// Process image
			for($i = 0; $i < count($_FILES["file"]["name"]); $i++)
				{
				 // Update row
					$filename = $_FILES["file"]["name"][$i];		
					$sql = "UPDATE properties ".
			   			   "SET str_image". ($i + 1) ."= '$filename' ".
			   			   "WHERE int_prop_ID = $property_id ";
					mysql_query($sql, $conn);		
		
				// Move file
				$tmp_filename = $_FILES["file"]["tmp_name"][$i];
				move_uploaded_file($tmp_filename, $folder ."/". $filename);
			   }

header( 'Location: view.php' );

?>
:?: :?: :?: :?:

Posted: Tue Jun 12, 2007 3:55 am
by Dale
I personally use copy(); instead of move_uploaded_files();.

Try this:

Code: Select all

copy ($_FILES['file']['tmp_name'], "$folder/".$_FILES['file']['name']) or die ("Could not copy");
Instead of...

Code: Select all

move_uploaded_file($tmp_filename, $folder ."/". $filename);

Posted: Tue Jun 12, 2007 4:53 am
by volka
$_FILE..['name'] contains user-input, it can include .. and ~ and all those other nasty special characters. Better use basename()

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', true);
for($i = 0; $i < count($_FILES["file"]["name"]); $i++)
{
  $filename = $_FILES["file"]["name"][$i];
  $sql = "UPDATE
      properties
    SET
      str_image" . $i+1 . "='$filename'
    WHERE
      int_prop_ID = $property_id
    ";
  echo '<div>Debug: ', htmlentities($sql), "</div>\n";
  mysql_query($sql, $conn) or die(mysql_error());
  echo '<div>Debug: ', mysql_affected_rows($conn), " rows affected</div>\n";
  
  $source = $_FILES["file"]["tmp_name"][$i];
  $target = $folder ."/". basename($filename);
  echo "<div>Debug: $source - $target</div>\n";
  if ( !move_uploaded_file($source, $target) ) {
    echo '<div>failed to move file</div>';
  }
}
// header( 'Location: view.php' );

for rename file it's work, can I ask more..??

Posted: Tue Jun 12, 2007 9:12 am
by aris1234
thanks before volka... its very helpful

your code for rename file its work, but i have problem again ..?? :D

old file can't delete, how to delete old file, i want change with new file? :wink:

how to modification that code..?? :roll:

Posted: Tue Jun 12, 2007 10:02 am
by superdezign
You delete files by using unlink().

Posted: Tue Jun 12, 2007 2:27 pm
by Benjamin
I voted for please.