You are here: Help with rename and unlink script please « PHP Programming Language « IT news, forums, messages
Help with rename and unlink script please

Posted by Dynamo on 10/27/88 11:48

I am trying to upload a file and then rename the file as something else.
Everything worked fine unless the renamed filename already existed. So I added
some extra code to check if the filename already existed and if so to delete it
before renaming the uploaded file. Everything now works fine in the following
code:

$uploaddir = 'images/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
$imagefilename = $_POST['imagefilename'];
$renamedfile = $uploaddir . $imagefilename;
if (file_exists($renamedfile)) {
unlink($renamedfile);
}
rename($uploadfile , $renamedfile);
}
else {
echo "There was an error in your file upload. \n";
echo 'Here is some more debugging info:';
print_r($_FILES);
}

That seems like a lot of code to do something relatively simple. So my question
is, is there a simpler way like forcing the rename function to overwrite an
existing file with the same filename if it exists.

Regards
Dynamo

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация