Laravel rename file - php

I'm trying to rename a file in laravel 5.6
This works fine when I'm physically renaming the file, but when I'm just changing case - It throws an error:
League\Flysystem\FileExistsException: File already exists at path
Example
old path: Music/The Corrs/The Corrs - What Can I Do.mp3
new path (DO): Music/The Corrs/The Corrs - What Can I DO.mp3
Code
Storage::disk($disk)->move($old, new);
How can I work around this so I can rename/move files if just the case is changing?

On Windows, case doesn't matter for file names. So files with the names uppercase.txt and UPPERCASE.txt are the same, but it is impossible to have two files with the same name in a folder.
When renaming, the file is actually "moved" to another name. However the file already exists, so you get an error.
I would recommend to store the files in a temp folder first to rename or use a temporary name for the files before renaming.

Off the cuff solution
$oldPath = "Music/The Corrs/The Corrs - What Can I Do.mp3";
$newPath = "Music/The Corrs/The Corrs - What Can I DO.mp3";
$paddedPath = str_replace(" ", "##padding##", $oldPath);
//move from old path to padded path
Storage::move(
$oldPath,
$paddedPath,
);
//move from padded path to new
Storage::move(
$paddedPath,
$newPath
);

Related

How to allow double forward slashes when upload file using Laravel League s3

There is a directory in S3 bucket named uploads// and I want to upload my files there since it is already using in existing web app and when I tried to upload to uploads// with Laravel league, it is ignoring one slash from two. So I added /// and it is also ignored and file uploaded to a new folder with uploads.
Example:
I want the file to be uploaded as
uploads//attachments/filename.jpg
Currently one slash ignored and uploaded as
uploads/attachments/filename.jpg
Here is the relevant code snippet:
// assume path as '/attachments/filename.jpg'
if($isPathDifferent==0){
$path = 'uploads//'. $path;
}
$upload = Storage::disk('s3')->store($path, file_get_contents($file));
Storage::disk('s3')->setVisibility($path, 'public');
Please note that I cannot change the name uploads// because it has lot of resources and usage.
Please apply following solution, it should fix your problem
$path = Storage::disk('s3')->put('uploads/', $file);

php move_uploaded_file not creating file

I am having a problem with move_uploaded_file().
I am trying to upload a image path to a database, which is working perfectly and everything is being uploaded and stored into the database correctly.
However, for some reason the move_uploaded_file is not working at all, it does not produce the file in the directory where I want it to, in fact it doesn't produce any file at all.
The file uploaded in the form has a name of leftfileToUpload and this is the current code I am using.
$filetemp = $_FILES['leftfileToUpload']['tmp_name'];
$filename = $_FILES['leftfileToUpload']['name'];
$filetype = $_FILES['leftfileToUpload']['type'];
$filepath = "business-ads/".$filename;
This is the code for moving the uploaded file.
move_uploaded_file($filetemp, $filepath);
Thanks in advance
Try this
$target_dir = "business-ads/";
$filepath = $target_dir . basename($_FILES["leftfileToUpload"]["name"]);
move_uploaded_file($_FILES["leftfileToUpload"]["tmp_name"], $filepath)
Reference - click here
Try using the real path to the directory you wish to upload to.
For instance "/var/www/html/website/business-ads/".$filename
Also make sure the web server has write access to the folder.
You need to check following details :
1) Check your directory "business-ads" exist or not.
2) Check your directory "business-ads" has permission to write files.
You need to give permission to write in that folder.
make sure that your given path is correct in respect to your current file path.
you may use.
if (is_dir("business-ads"))
{
move_uploaded_file($filetemp, $filepath);
} else {
die('directory not found.');
}

Get file from temp after confirm with PHP/Laravel

I have a form with a file to uplaod. All works find. But I don't want to move the file directly into a folder.
After submit I show a confirm page and there I show the uploaded file with
header('Content-Type: image/x-png');
$file = file_get_contents(\Illuminate\Support\Facades\Input::file('restImg'));
$imgType = \Illuminate\Support\Facades\Input::file('restImg')->guessClientExtension();
echo sprintf('<img src="data:image/png;base64,%s" style="max-height: 200px"/>', base64_encode($file));
This works fine. After the confirmation I like to move the file to a folder. How can I move the file after the confirmation? The Input::get('file') is not available anymore.
You will have to store the file in the initial upload somewhere temporarily other than the default tmp directory.
The documentation for PHP file uploads says:
The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed
This means that moving onto the next request, the file will no longer be available.
Instead, move it to your own custom temp directory or rename it to something special, then keep the filename in the $_SESSION to persist it to the next request.
For Laravel, this should mean putting it in the /storage directory with something like this:
// Get the uploaded file
$file = app('request')->file('myfile');
// Build the new destination
$destination = storage_path() . DIRECTORY_SEPARATOR . 'myfolder';
// Make a semi-random file name to try to avoid conflicts (you can tweak this)
$extension = $file->getClientOriginalExtension();
$newFilename = md5($file->getClientOriginalName() . microtime()).'.'.$extension;
// Move the tmp file to new destination
app('request')->file('myfile')->move($destination, $newFilename);
// Remember the last uploaded file path at new destination
app('session')->put('uploaded_file', $destination.DIRECTORY_SEPARATOR.$newFilename);
Just remember to unlink() the file after the second request or do something else with it, or that folder will fill up fast.
Additional Reference:
http://api.symfony.com/2.7/Symfony/Component/HttpFoundation/File/UploadedFile.html

php tempnam(sys_get_temp_dir(), $fileName); saves in wierd name

i'm using to tempnam(sys_get_temp_dir(), $fileName); to temporary save an image in the /private/var/tmp.
The images are being save with a weird text after them
For example:
10153114610662388_1434185314.jpggd5Wc6
After i'm saving them, I'm uploading them to facebook and it gives me the following error {"error":"(#324) Requires upload file"} and I think it because of that.
From the PHP documentation (http://php.net/manual/en/function.tempnam.php) for tempnam:
string tempnam ( string $dir , string $prefix )
dir The directory where the temporary filename will be created.
prefix The prefix of the generated temporary filename.
So the second parameter is a prefix for the filename, not the filename that it will use. tempnam makes sure that the filename is unique (so you don't overwrite another temp file) - and that's what the "weird text" at the end is for.
If you want to save the file with the filename you already have just use it directly - but understand that if a file with that name already exists you'll overwrite it.

How we can read zip file and get information of files or folders contains without unzipping in PHP?

What I actually wanted to do is read zip file and then if it does contain folder then refuse it with some message.
I want user should upload zip file with files only without any directory structure.
So I want to read zip file contains and check file structure.
I am trying with following code snippet.
$zip = zip_open('/path/to/zipfile');
while($zip_entry = zip_read($zip)){
$filename = zip_entry_name($zip_entry);
//#todo check whether file or folder.
}
I have sorted out.
I am now checking filename as strings wherever I am getting string ending with "/" that am treating as directory else as file.
can't you parse path of $filename? something like $dirName = pathinfo($filename, PATHINFO_DIRNAME)

Categories