Jump to content

Recommended Posts

I have this code

 

<?php
$imagename = $_GET['imagename'];
	$renjpg = $iname[0].".jpg";
	rename("../../UserFiles/Image/layouts/$imagename" , "../../UserFiles/Image/layouts/$renjpg");

                 // echo "<br> RENAMED PIC";
	// echo "<br>image = ".$imagename;
	// echo "<br>NEW image = ".$renjpg."<br>";
?>

 

There is something wrong with my syntax in the rename, because I can't rename it! I tried a dozen things but couldn't find how to write the $iname[0].".jpg"..

 

Please help me, I've spend hours the last days trying to find out what the right syntax will be. I am stuck to this thing  >:(

Link to comment
https://forums.phpfreaks.com/topic/52490-cant-rename-a-dynamic-name/
Share on other sites

when running it on php5 what os are you using????

if its windows, the directory structure differs from linux

instead of

rename("../../UserFiles/Image/layouts/$imagename" , "../../UserFiles/Image/layouts/$renjpg");

try using

rename("..\..\UserFiles\Image\layouts\$imagename" , "..\..\UserFiles\Image\layouts\$renjpg");

 

 

greetz

 

Silverado

thank you Silverado_NL for your concern,

 

Although I was sure that the slashes had nothing to do with my issue i did what you said and nothing changed. When I have something simple like this works :

 

<?php
$imagename = "test.JPG";
	$renjpg = "renamed_test.jpg";
	rename("../../UserFiles/Image/layouts/$imagename" , "../../UserFiles/Image/layouts/$renjpg");

                 // echo "<br> RENAMED PIC";
	// echo "<br>image = ".$imagename;
	// echo "<br>NEW image = ".$renjpg."<br>";
?>

So my conclusion was that the syntax has a problem. Maybe my php.ini need modification or in PHP5.0 there is an alternative way to write this simple thing. Even if the thing with slashes worked it would be a problem because when i finish it I'll upload it to a linux server so I must rewrite/change those lines again.

 

 

I am really confused.

 

try this.

 

it may releave something

 

change

<?php
rename("../../UserFiles/Image/layouts/$imagename" , "../../UserFiles/Image/layouts/$renjpg");
?>

 

to

 

<?php
$fFrom = "../../UserFiles/Image/layouts/$imagename";
$fTo = "../../UserFiles/Image/layouts/$renjpg";
echo "from: $fFrom<br>to: $fTo";
rename($fFrom , $fTo);
?>

 

post the from: and to: on the results that fail (also one what that works)

 

it also could be directory structor

Yes Ken, this line is causing the problem. The $iname is the result of an explode of a get variable that has a full filename. In the code below you can see what I mean

 

I also tried this and again didn't work

 

<?php
// get the variable - its a $_GET thing in real life
$imagename1="TEST.JPG";
$iname2 = explode(".",$imagename1); //get the filename without extension
$renjpg = $iname2[0].".jpg"; // add the extension .jpg
// $renjpg = "TEST.JPGI"; WHEN THIS LINE IS UN commented it works!

	echo "original picture ".$imagename." renamed to ".$renjpg;

	//check if imagename exist in this path
	if(file_exists($imagename)) echo "<p>FILE EXIST</p>";

	//rename($imagename , $renjpg);
	rename("$imagename1" , "$renjpg");
	if(rename($imagename1 , $renjpg)) {echo "<br><br>DONE!";}else{echo "<br><br>PROBLEM";}		
?>

 

After a lot of searching and tests I think that I found it.

 

I recopy the "working" script

 

<?php

// get the variable - its a $_GET thing in real life
$imagename="TEST.JPG";
$iname2 = explode(".",$imagename); //get the filename without extension
$renjpg = $iname2[0].".jpg"; // add the extension .jpg
$tempjpg = "temp.jpg";
// $renjpg = "TEST.JPGI"; WHEN THIS LINE IS UN commented it works!


	//check if imagename exist in this path
	//if(file_exists($imagename1)) echo "<p>FILE EXIST</p>";

	rename($imagename,$renjpg);


?>

 

This code DOESN'T WORK on windows. The reason is because windows are case Insesitive, so when you want to change TEST.JPG to TEST.jpg, the execution happens but the file system seems intact because for windows ".jpg" and ".JPG" is the same.

 

The above code works on a linux server.

 

Also found those information:

 

rename() definitely does not follow the *nix rename convention on WinXP with PHP 5.  If the $newname exists, it will return FALSE and $oldname and $newname will remain in their original state.  You can do something like this instead:

 

function rename_win($oldfile,$newfile) {

  if (!rename($oldfile,$newfile)) {

      if (copy ($oldfile,$newfile)) {

        unlink($oldfile);

        return TRUE;

      }

      return FALSE;

  }

  return TRUE;

}

 

There are also many more on php.net about the issue of rename function under Windows. ( try searching about rename())

 

Any thoughts or concerns would be great. My script will not work locally but currently its fine by me because the final script will be uploaded to a linux server.

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.