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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aris1234
Forum Newbie
Posts: 11
Joined: Mon May 07, 2007 7:10 am

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

Post 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' );

?>
:?: :?: :?: :?:
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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);
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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' );
aris1234
Forum Newbie
Posts: 11
Joined: Mon May 07, 2007 7:10 am

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

Post 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:
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You delete files by using unlink().
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I voted for please.
Post Reply