Why does the rename function not work?
$new_image_name = $maximum_id + 1 . '.jpg';
$old_image_name = $file_name;
// Rename the image file
rename($profile_folder . $old_image_name, $profile_folder . $new_image_name);
var_dump($profile_folder . $old_image_name, $profile_folder . $new_image_name);
gives me string(56) "/var/www/myproject/public/images/1/profile/old.jpg" string(50) "/var/www/myproject/public/images/1/profile/1.jpg"
var_dump(rename($profile_folder . $old_image_name, $profile_folder . $new_image_name));
returns (bool)false
The paths are correct, however the file is not renamed and uploaded with the old name. What am I doing wrong?
Related
In laravel 8, I updated the storage path for image uploads. It was app/violations/filename.jpeg
if( $request->hasfile('violationStatement') )
{
$file = $request->file('violationStatement');
$extension = $file->getClientOriginalExtension();
$filename = $violation->plateNumber . '-' . $violation->violationType . '.' . $extension;
$file->move('app/violations/', $filename);
$violation->violationStatement = $filename;
}
And it was updated to app/violations/speeding/Toyota/XXX-1234/11-June-2022/11-26 AM/filename.jpeg
if( $request->hasfile('violationStatement') )
{
$file = $request->file('violationStatement');
$extension = $file->getClientOriginalExtension();
$filename = 'violation-statement' . '-' . $violation->plateNumber . '-' . $violation->violationType . '.' . $extension;
$file->move('app/violations/' .
$violation->violationType . '/' .
$violation->carModel . '/' .
$violation->plateNumber . '/' .
Carbon::parse($violation->violationDateTime)->format('d-M-Y/H-i A'), $filename);
$violation->violationStatement = $filename;
}
The images are displayed in datatables, so obviously I'll have to change the image src to the new URL. When I do so, how can I also display the old images of the old directory?
I'm trying to get this path:
C:\xampp\htdocs\site\folder\filename.jpg
By doing:
$i = new Imagick('C:\xampp\htdocs\site\folder' . DIRECTORY_SEPARATOR . $filename);
This returns:
C:\xampp\htdocs\site\folder\
But it doesn't have the $filename.
I have also tried:
$i = new Imagick(__DIR__ . DIRECTORY_SEPARATOR . 'folder' . DIRECTORY_SEPARATOR . $filename);
This returns:
C:\xampp\htdocs\site\files\folder\filename.jpg
So I tried adding ..\ to get away from the files folder:
$i = new Imagick(__DIR__ . DIRECTORY_SEPARATOR . '..\folder' . DIRECTORY_SEPARATOR . $filename);
But that returns:
C:\xampp\htdocs\site\files\..\folder\filename.jpg
How can I get to the correct folder?
$filename = 'filename.jpg';
$i = new Imagick('C:\xampp\htdocs\site\folder' . DIRECTORY_SEPARATOR . $filename);
This should give the correct value
Having some trouble with rewriting a photo file. I need the file name to get rewritten as a random string. The file uploads fine - I can't seem to get it copy the file and rewrite the file name to the random string. The file is going to stay in the directory.
The function is working fine and I can rewrite file name in the database, but it will not rewrite the actual file in the folder. The folder permissions are rwxr-xr-x (755).
Any thoughts?
function AfterUpdate(){
$file = $this->file_attachment;
$path_parts = pathinfo($file);
$newFilename = $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];
$file_src = $_SERVER['DOCUMENT_ROOT'] . $file;
$newfile_src = $_SERVER['DOCUMENT_ROOT'] . $newFilename;
if (move_uploaded_file($file_src, $newfile_src)){
$this->file_attachment = $newFilename;
}
}
$newFilename contains a path location I guess by looking at the $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];.
$newFilename should just be the new file name with extension.
move_uploaded_file will only move files from one folder to another or the same, that already exists. But will not create a folder for you.
Simple fix. Replace move_uploaded_file with rename. The file will not be moved, just renamed.
$file = $this->file_attachment;
$path_parts = pathinfo($file);
$newFilename = $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];
$file_src = $_SERVER['DOCUMENT_ROOT'] . "/" . $file;
$newfile_src = $_SERVER['DOCUMENT_ROOT'] . "/" . $newFilename;
if (rename($file_src, $newfile_src)){
$this->file_attachment = $newFilename;
}
I am uploading multiple images within a single category and I would like to store each group of images in a unique directory within my 'images/' directory as follows:
'images/unique_category/image1.jpg'
I have the following code but it is not creating a directory. I suspect it has something to do with setting the recursive parameter as 'true' which I believe I have done. Am I using the mkdir function incorrectly?
Thank you!
$unique_directory = "../images/".$_POST['item_name'];
if (is_dir($unique_directory)
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$unique_directory."/".$_FILES["file"]["name"]);
echo "Stored in: " . $unique_directory."/".$_FILES["file"]["name"];
}
else
{
mkdir($unique_directory, 0777, true);
move_uploaded_file($_FILES["file"]["tmp_name"],
$unique_directory."/".$_FILES["file"]["name"]);
Here, give this a try. I tested it on my (hosted) server and it works. Yet, I tested it by placing the files in the root of it, and used images instead of ../images for the $unique_directory variable.
I also used the chmod command apart from the other function, because the other method did not work.
N.B.: If possible, try changing 0777 to 0755, because using 0777 is not the safest setting.
<?php
$filename = $_POST['item_name'];
$unique_directory = "../images";
if (!is_dir($unique_directory . '/' . $filename)){
mkdir($unique_directory . "/" . $filename);
chmod("$unique_directory" . "/" .$filename, 0777);
}
if (is_dir($unique_directory))
{
move_uploaded_file($_FILES['file']['tmp_name'], $unique_directory . "/" . $filename . "/" . $_FILES['file']['name']);
echo "1) Stored in: " . $unique_directory . "/" . $filename . "/" . $_FILES['file']['name'];
}
else
{
move_uploaded_file($_FILES['file']['tmp_name'], $unique_directory . "/" . $filename . "/" . $_FILES['file']['name']);
echo "2) Stored in: " . $unique_directory . "/" . $filename . "/" . $_FILES['file']['name'];
}
?>
I am uploading three files using Zend Element File. I am uploading and renaming the files. Now the problem is that when uploading same extension, it generates error of
Zend_Filter_Exception: File 'D:\wamp2\tmp\php2443.tmp' could not be renamed. It already exists.
For example if in first file I upload file of extenstiono .txt in second I upload .docx and in third I again select .txt or .docx, It will generate the above given error.
But If I select three different extensions, every thing goes best. I am using the following code
if ($form->med_file_1->isUploaded()) {
$originalFilename = pathinfo($form->med_file_1->getFileName());
$newFilename = time() . '.' . $originalFilename['extension'];
$form->med_file_1->addFilter('Rename', "application_data/uploaded_files/patients/" . $newFilename,$originalFilename['basename']);
$form->med_file_1->receive();
}
if ($form->med_file_2->isUploaded()) {
$originalFilename = pathinfo($form->med_file_2->getFileName());
$newFilename = time() . '.' . $originalFilename['extension'];
$form->med_file_2->addFilter('Rename', "application_data/uploaded_files/patients/" . $newFilename,$originalFilename['basename']);
$form->med_file_2->receive();
}
if ($form->med_file_3->isUploaded()) {
$originalFilename = pathinfo($form->med_file_3->getFileName());
$newFilename = time() . '.' . $originalFilename['extension'];
$form->med_file_3->addFilter('Rename', "application_data/uploaded_files/patients/" . $newFilename,$originalFilename['basename']);
$form->med_file_3->receive();
}
The reason for the error is because you are naming each uploaded file:
time() . '.' . $originalFilename['extension'];
The call to receive() happens so fast that time() returns the same value on each call so you can end up with duplicate file names. You just need to generate a more unique name for each file. Something like the following should work:
md5(uniqid(time(), true)) . '.' . $originalFilename['extension'];
//or
$originalFilename['basename'] . '_' . time() . '.' . $originalFilename['extension'];