increase Posted May 9, 2022 Share Posted May 9, 2022 I am using this line of code to rename a file that has beeuploaded to my server rename("{$filePath}.part", $original_name); Where filePath is this file file_6278ea910fbf3 and the rename $original_name is the variable which contains the text ine below Genesis _ Mad Man Moon _ A Trick Of The Tail (2007 Remaster) HQ Audio (128 kbps).mp3 when the rename fuction completes the file file_6278ea910fbf3 disappears off the directory on the server, but if I use this rename("{$filePath}.part", "Genesis _ Mad Man Moon _ A Trick Of The Tail (2007 Remaster) HQ Audio (128 kbps).mp3"); It works perfectly How can I make the variable $original_name reflect the same text, is there a 'normlising' option in php? Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/ Share on other sites More sharing options...
gw1500se Posted May 9, 2022 Share Posted May 9, 2022 Did you echo $original_name to make sure it contains what you expect (you didn't show how you built it)? Do you have errors turned on? error_reporting(E_All) Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596062 Share on other sites More sharing options...
increase Posted May 11, 2022 Author Share Posted May 11, 2022 (edited) Thank you I managed to fix it, the problem was in the code Edited May 11, 2022 by increase Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596155 Share on other sites More sharing options...
increase Posted May 12, 2022 Author Share Posted May 12, 2022 (edited) I thought it was fixed but it isn't, this is a very strange issue Here is my code to get the original name // Check if file has been uploaded if (!$chunks || $chunk == $chunks - 1) { // Strip the temp .part suffix off rename("{$filePath}.part", $filePath); //start of name search $searchthis = 'filename'; $matches = array(); $handle = @fopen($filePath, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); if(strpos($buffer, $searchthis) !== FALSE) $matches = $buffer; } fclose($handle); } $word = $matches; $words = explode(' ', $word); foreach($words as $ws) { if(strlen($ws) > 0) { $new_word[] = $ws; } } $cnt = count($new_word); $cnt = $cnt-1; $line = $new_word[3]; $new_word[3] = str_replace('filename="', ' ', $new_word[3]); $new_word[3] = str_replace(' ', '', $new_word[3]); $new_word[$cnt] = str_replace('"', '', $new_word[$cnt]); array_shift($new_word); array_shift($new_word); array_shift($new_word); $original_name = implode(" ",$new_word); $new = str_replace('/home2/username/domains/domain_name/public_html/plupload//', '', $filePath); $src = "/home2/username/domains/nerjabible.com/public_html/plupload/$new"; $dest = "/home2/username/domains/nerjabible.com/public_html/path_to_the_file/Genesis _ Mad Man Moon.mp3"; $file1 = "src.txt"; $fp = fopen($file1, 'w'); fwrite($fp, $src); fclose($fp); $file2 = "dest.txt"; $fp = fopen($file2, 'w'); fwrite($fp, $dest); fclose($fp); rename($src, $dest); } the output from src.txt is /home2/username/domains/path_to_the_file/file_627d388928b7d and the output from dest.txt is /home2/username/domains/path_to_the_file/Genesis _ Mad Man Moon.mp3 but after the rename, the destination directory does not contain the file. but if I change $dest to $dest = "/home2/username/domains/path_to_the_file/Genesis _ Mad Man Moon.mp3"; it works perfectly Edited May 12, 2022 by increase More information given on details of execution of php script Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596180 Share on other sites More sharing options...
ginerjm Posted May 12, 2022 Share Posted May 12, 2022 1 - use the <> icon to post your code into a box of its own. Standard practice for forums. 2 - what does this line say to you? It doesn't say anything meaningful to me but I can be pretty clueless sometimes. if (!$chunks || $chunk == $chunks - 1) (Note how I posted this code) It seems to be checking if the value of $chunks is true OR if you can compare some value to a reduced value of $chunks. But as it is the beginning of a block of code we have to wonder what you are doing. The rest of the code is not worth wading thru since it is not that readable (as previously mentioned). Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596181 Share on other sites More sharing options...
ginerjm Posted May 12, 2022 Share Posted May 12, 2022 Ok I took the time to re-structure the code you posted and I haven't a clue what you are trying to do. Here's what it looks like when written with some sense of format and posted correctly: if (!$chunks || $chunk == $chunks - 1) { // Strip the temp .part suffix off rename("{$filePath}.part", $filePath); //start of name search $searchthis = 'filename'; $matches = array(); $handle = fopen($filePath, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); if(strpos($buffer, $searchthis) !== FALSE) $matches = $buffer; } fclose($handle); } $word = $matches; $words = explode(' ', $word); foreach($words as $ws) if(strlen($ws) > 0) $new_word[] = $ws; $cnt = count($new_word); $cnt = $cnt-1; $line = $new_word[3]; $new_word[3] = str_replace('filename="', ' ', $new_word[3]); $new_word[3] = str_replace(' ', '', $new_word[3]); $new_word[$cnt] = str_replace('"', '', $new_word[$cnt]); array_shift($new_word); array_shift($new_word); array_shift($new_word); $original_name = implode(" ",$new_word); $new = str_replace('/home2/username/domains/domain_name/public_html/plupload//', '', $filePath); $src = "/home2/username/domains/nerjabible.com/public_html/plupload/$new"; $dest = "/home2/username/domains/nerjabible.com/public_html/path_to_the_file/Genesis _ Mad Man Moon.mp3"; $file1 = "src.txt"; $fp = fopen($file1, 'w'); fwrite($fp, $src); fclose($fp); $file2 = "dest.txt"; $fp = fopen($file2, 'w'); fwrite($fp, $dest); fclose($fp); rename($src, $dest); } What is the significance of the 4th item of the new_word array? Have you considered adding some error checking for all of the operations you are performing? Did you even add the error checking that was suggested earlier? Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596182 Share on other sites More sharing options...
increase Posted May 12, 2022 Author Share Posted May 12, 2022 (edited) 14 minutes ago, ginerjm said: Ok I took the time to re-structure the code you posted and I haven't a clue what you are trying to do. Here's what it looks like when written with some sense of format and posted correctly: if (!$chunks || $chunk == $chunks - 1) { // Strip the temp .part suffix off rename("{$filePath}.part", $filePath); //start of name search $searchthis = 'filename'; $matches = array(); $handle = fopen($filePath, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); if(strpos($buffer, $searchthis) !== FALSE) $matches = $buffer; } fclose($handle); } $word = $matches; $words = explode(' ', $word); foreach($words as $ws) if(strlen($ws) > 0) $new_word[] = $ws; $cnt = count($new_word); $cnt = $cnt-1; $line = $new_word[3]; $new_word[3] = str_replace('filename="', ' ', $new_word[3]); $new_word[3] = str_replace(' ', '', $new_word[3]); $new_word[$cnt] = str_replace('"', '', $new_word[$cnt]); array_shift($new_word); array_shift($new_word); array_shift($new_word); $original_name = implode(" ",$new_word); $new = str_replace('/home2/username/domains/domain_name/public_html/plupload//', '', $filePath); $src = "/home2/username/domains/nerjabible.com/public_html/plupload/$new"; $dest = "/home2/username/domains/nerjabible.com/public_html/path_to_the_file/Genesis _ Mad Man Moon.mp3"; $file1 = "src.txt"; $fp = fopen($file1, 'w'); fwrite($fp, $src); fclose($fp); $file2 = "dest.txt"; $fp = fopen($file2, 'w'); fwrite($fp, $dest); fclose($fp); rename($src, $dest); } What is the significance of the 4th item of the new_word array? Have you considered adding some error checking for all of the operations you are performing? Did you even add the error checking that was suggested earlier? Thank you for doing that, I actually posted the code at the request of another members post. what i am asking is very simple, when I use the rename function, to move a file from one location to another, with a new name, the new file does not appear in the destination directory so rename($src, $dest); does not work even though the $src and $dest are shown to be correct from the two text files I included scr.txt and dest.txt at the end of the rename the file disappears from the source directory but does not appear in the destination directory. But if I do this rename($src, "the_new_name_of_the_file.mp3"); it works perfectly Edited May 12, 2022 by increase Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596184 Share on other sites More sharing options...
ginerjm Posted May 12, 2022 Share Posted May 12, 2022 Ok - so start doing some echo's to show EXACTLY what is happening and be sure the error checking is enabled and turned on. That way you will know if any of your assumptions are incorrect. It's called debugging. Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596185 Share on other sites More sharing options...
increase Posted May 12, 2022 Author Share Posted May 12, 2022 Forgetting about all the preceding code and keeping it simple 1.set $src = exisitng file. 2. Set $dest = new name. $fileMoved = rename($src, $dest); if($fileMoved){ echo 'Success!'; } the $src file disappears in the directory but the $dest file does not appear, would be really grateful for some suggestion as to the problem with rename I cannot get rename to work with two variable and a change of name in the $dest variable If I use rename($src, "temporary.mp3"); it works Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596187 Share on other sites More sharing options...
mac_gyver Posted May 12, 2022 Share Posted May 12, 2022 if the rename() call is failing, there would be a php error message. do you have php's error_reporting set to E_ALL and display_errors set to ON, preferably in the php.ini on your system, so that php will report and display all the errors it detects? 7 minutes ago, increase said: 2. Set $dest = new name. what is the actual file name used, so that someone could determine if something about it is not permitted or if it's actually being moved to somewhere you are not expecting? Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596188 Share on other sites More sharing options...
ginerjm Posted May 13, 2022 Share Posted May 13, 2022 And you still did not take the advice given. ADD some echo statements to make SURE that what you think is happening is really what you are asking for. Jeez! Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596190 Share on other sites More sharing options...
increase Posted May 13, 2022 Author Share Posted May 13, 2022 (edited) Yes I did take your advice with echoes throughout and error reporting at the top of the script ini_set('display_errors',1); error_reporting(E_ALL); here is the src.txt and dest txt dest.txt src.txt Edited May 13, 2022 by increase Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596194 Share on other sites More sharing options...
ginerjm Posted May 13, 2022 Share Posted May 13, 2022 Show us the code, properly posted here of course Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596198 Share on other sites More sharing options...
increase Posted May 13, 2022 Author Share Posted May 13, 2022 23 minutes ago, ginerjm said: Show us the code, properly posted here of course Which code do you want? Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596199 Share on other sites More sharing options...
Solution increase Posted May 13, 2022 Author Solution Share Posted May 13, 2022 (edited) I have it! Instead of trying to change the filename back to the original filename after the processing, there was an option buried in the form data javascript, that cariies the filname into the upload.php file FilesAdded: function(up, files) { plupload.each(files, function(file) { document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>'; }); }, FilesAdded: function(up, files) { plupload.each(files, function(file) { document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>'; up.setOption('url', 'upload.php?name=' + file.name); }); }, By adding the line up.setOption('url', 'upload.php?name=' + file.name); to the function it picks up the original filename and processes that instead of the file code name. Works perfectly, thanks for your help Edited May 13, 2022 by increase typos Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596200 Share on other sites More sharing options...
ginerjm Posted May 13, 2022 Share Posted May 13, 2022 "form data javascript" Something that you NEVER EVER MENTIONED! Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596201 Share on other sites More sharing options...
increase Posted May 13, 2022 Author Share Posted May 13, 2022 1 hour ago, ginerjm said: "form data javascript" Something that you NEVER EVER MENTIONED! Because I had spent many hours trying all different ways to pass the name data from the form input to php, the commented out option was hard to find for me. Still it is the end result that matters, which is now problem resolved. Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596203 Share on other sites More sharing options...
ginerjm Posted May 13, 2022 Share Posted May 13, 2022 Passing data from an input tag is simply a matter of declaring a form with POST and and action to your script. Then in your script you grab the $_POST element from the tag's 'name' attribute and use it. Real Simple. You don't need JS to do it necessarily. Reading a book makes a world of difference. Quote Link to comment https://forums.phpfreaks.com/topic/314774-php-rename-function-cannot-rename-file/#findComment-1596206 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.