Jump to content

Php rename function cannot rename file


increase
Go to solution Solved by increase,

Recommended Posts

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?

Link to comment
Share on other sites

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 by increase
More information given on details of execution of php script
Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by increase
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

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 by increase
typos
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.