Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is php rename() corrupting my file?

I am using plupload to do an upload of multiple files to my server. Using this, there is a parameter 'url : 'upload.php'. upload.php catches the files as they are received, and might recombine them if they get chunked. Once the full file is received, it sends a response back to the original page, displaying a green checkbox icon.

I have added some code to this page, after all the main code to manipulate the photos I have uploaded. My plan is to create three copies of my full size image, lg, med, and small. I got this part working, but then decided to first rename the original file to match my naming scheme.

I now get a corrupted renamed file, and thus my three smaller images also get corrupted.

//get the original file info
$filepath = $_SERVER['DOCUMENT_ROOT'].'/uploads/';
$filepathinfo = pathinfo($filepath.$fileName);//fileName is used previously in the file

//rename original file to a unique name
$finding_id = 'xyz';
$file_name_new = uniqid($client_id . '-' . $finding_id . '-', true); //doesn't include extension
//rename($filepath.$fileName, $filepath.$file_name_new.'.'.$ext);
//copy($filepath.$fileName, $filepath.$file_name_new.'.'.$ext);

As is, I get my one file, or how ever many I uploaded, byte size matches original exactly, and name stays the same (except for removal of certain characters).

If I uncomment only the rename function, I actually get two files. The byte sizes total the original photo. The larger file displays with a section of gray at the bottom. The smaller file doesn't display at all.

If I uncomment only the copy function, I get an exact renamed copy of my original file, my original file, and another file, the same size and corruption as the larger file doing a rename.

Any ideas? Seems like it should be pretty straightforward.

like image 929
Bob Avatar asked May 05 '11 21:05

Bob


People also ask

What is the purpose of the rename () function when creating a file PHP?

The rename() function in PHP is an inbuilt function which is used to rename a file or directory. It makes an attempt to change an old name of a file or directory with a new name specified by the user and it may move between directories if necessary.

How to rename files with PHP?

PHP rename() Functionrename("images","pictures"); rename("/test/file1. txt","/home/docs/my_file. txt");

How do you rename the file if already exists in PHP?

Solution: We need to rename by appending increment number to the filename if file exists using PHP file_exists() function.

How do you rename a file?

Find and select the file, then select File > Rename. Type the new name and press Enter.


2 Answers

  1. if the file was currently uploaded by HTTP POST use move_uploaded_file
  2. if you fopen() somewhere in this request the same file make sure to call fclose()
like image 183
k2s Avatar answered Oct 16 '22 09:10

k2s


I forgot I had the chunking feature turned on. Must have turned it on to test something. For whatever reason, when the script was running the last chunk of the file hadn't been fully appended yet. Thanks for all the input anyway!

like image 29
Bob Avatar answered Oct 16 '22 09:10

Bob