Page 1 of 1
Simple Upload Form + Change filename
Posted: Sun Mar 18, 2007 1:45 pm
by thiscatis
I's experimenting with form uploads,
I'm using this code:
Code: Select all
if($_POST['Submit']) {
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
} }
I'd like to change filename when the file is uploaded to a predefined variable.
How do I do this? I think the tmp_name needs to change somehow but I have no clue how...
Re: Simple Upload Form + Change filename
Posted: Sun Mar 18, 2007 2:07 pm
by tecktalkcm0391
Not tested but you can try this:
Code: Select all
if($_POST['Submit']) {
$target_path = "uploads/";
$newname = time();
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path) && rename($target_path.$_FILES['uploadedfile']['tmp_name'], $target_path.$newname)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
} }
Posted: Sun Mar 18, 2007 2:23 pm
by feyd
tmp_name doesn't need to change, the $target_path needs to change.
Posted: Sun Mar 18, 2007 2:57 pm
by thiscatis
hey feyd,
i did it like this now:
Code: Select all
if($_FILES[picture_1][name] !="") {
$image_name = $_FILES[picture_1][name] ;
$extension = ".jpg";
$newimg_Name = "$m_id" . $extension; // m_id is defined before this snippet
$newimg_Path = "modules/uploads/" . $newimg_Name;
move_uploaded_file($_FILES[picture_1][tmp_name], $newimg_Path);
chmod($newimg_Path, 0777);
}
And it works!
But an additional question,
is it possible to upload if to 2 different locations at once?
adding a move_uploaded_file doesn't work.. also adding the entire code again with a new $newimg_Path
Posted: Sun Mar 18, 2007 3:45 pm
by feyd
Quote your named array indices.
Once you perform
move_uploaded_file() you will need to
copy() to create another instance of the file.
Posted: Sun Mar 18, 2007 3:51 pm
by thiscatis
can it be copied to a directory lower than the one the php script is in?
If so, do I need to use /../dir/ or ../dir/ ?
Posted: Sun Mar 18, 2007 3:54 pm
by feyd
Try it.
Posted: Sun Mar 18, 2007 4:10 pm
by thiscatis
Code: Select all
if($_FILES[picture_1][name] !="") {
$image_name = $_FILES['picture_1']['name'] ;
$extension = ".jpg";
$newimg_Name = "$m_id" . $extension;
$newimg_Path = "modules/models/" . $newimg_Name;
move_uploaded_file($_FILES['picture_1']['tmp_name'], $newimg_Path);
chmod($newimg_Path, 0777);
if (!copy($newimg_Name, "../modules/models/$newimg_Name")) {
echo "Error. Couldn't copy that file.\n";}
}
Keeps giving me "Error. Couldn't copy that file".
Is the way I set the new path correctly?
I had a look at rename() but that removes the original picture from the directory too.
Maybe first a
Code: Select all
if (!copy($newimg_Name, "$newimg_Name.bak")) {
echo "Error. Couldn't copy that file.\n";}
and then a
or is there a better way to do this?
Posted: Sun Mar 18, 2007 4:16 pm
by feyd
You're missing quotes for a new more named indices.
The copy call needs $newimg_Path, not $newimg_Name, for the first parameter.