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
aris1234
Forum Newbie
Posts: 11 Joined: Mon May 07, 2007 7:10 am
Post
by aris1234 » Tue Jun 12, 2007 3:49 am
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
//С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 » Tue Jun 12, 2007 3:55 am
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);
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Jun 12, 2007 4:53 am
$_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
Post
by aris1234 » Tue Jun 12, 2007 9:12 am
thanks before volka... its very helpful
your code for rename file its work, but i have problem again ..??
old file can't delete, how to delete old file, i want change with new file?
how to modification that code..??
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Jun 12, 2007 2:27 pm
I voted for please.