Link to home
Start Free TrialLog in
Avatar of mikearmas
mikearmas

asked on

Display and rename that image in php

i am trying to display all the image from a dir and let the use lick on a image that takes them to other page where they can rename that image.

or let them rename the image from the first page.

here is what i have the first code.

Then here is the php

it's just display the name of the file and the new name i want to give it. I can do the php rename of the file

But it only does the last image in the dir.

How can i make the first code work so if the user clicks on the edit button it will pull only that image file name to the data2file.php and then from the data2file.php it will rename the file name
<?php 
SESSION_START(); 
?>¿ 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<meta name="author" content="vs" /> 
<meta name="robots" content="index, follow" /> 

<title></title> 
</head> 
<body> 

<div id="centerColumn"> 
<div id="header"> 

</div>
<p><span class="small"></span><br /> 


</p><br> 


<form method="post" action="data2file.php"> 
<?php 

echo '
            
<table> 
<tr><td>'; 
     $username = $_SESSION['user']; 
        $url = $username."./"; 
        $handle = opendir ($url); 
        while (false !== ($file = readdir($handle))) { 
            if($file != "." && $file != ".." && $file != basename(__FILE__)) {                    
                                                             
                  //echo '<table border=1><tr><td>';                    
                   echo '<a href="edit.php" ><img border=1 src="'.$file.'"></a>'; 
                   echo '<br />'; 
                    
                   echo "<input type=submit name=$file value=Edit>" ; 
                  print $file; 
                  echo '<br>'; 
                  
                   echo "Enter new name: <input type='text'  name='new-name' length='30'>
                   <textarea type=text size='1' height='1'style='display:none' name='file-name' >". $file ."</textarea>"; 
                  
                  echo '<hr>'; 
                  //echo '</td></tr></table>';            
            
            } 
        } 
echo '</td>'; 

echo ' 

</table>'; 
 
?> 

</body> 
</html> 








<?php 
SESSION_START(); 


echo $_POST['new-name'];
echo '<br>';
echo $_POST['file-name']; 
echo '<br>';
echo "Successfully!"; 
?>

Open in new window

Avatar of Duboux
Duboux

Well.. you should create a new form with every image (one of the many options)

start loop {
      echo '
      <form method="post">
      <img src="images/'.$image_name.'" />
      <input type="hidden" name="name_orig" value="'.$image_name.'" />
      <input type="text" name="name_new" />
      <input type="submit" name="rename" value="rename" />
      </form>
      ';
} end loop


Then start the page with:
if ( (isset($_POST["rename")) && (isset($_POST["name_orig"])) && (!empty($_POST["name_orig"])) && (isset($_POST["name_new"])) && (!empty($_POST["name_new"])) ) {
      // check file names on bad characters...
      // do the renaming part...
}
You cannot have multiple submits inside the form, so either you do

<form>
{LOOP}
<input type="submit" value="send" />
</form>

to send all values at the same time to rename, or

{ // LOOP
<form>
...
<input type="submit" value="send" />
</form>
}
ASKER CERTIFIED SOLUTION
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And the rename file:

<?php 
SESSION_START(); 


echo $_POST['new-name'];
echo '<br>';
echo $_POST['file-name']; 

rename($_POST['file-name'], $_POST['new-name']);

echo '<br>';
echo "Successfully!"; 
?>

Open in new window

Avatar of mikearmas

ASKER

Thank you that works great
well i spoke to soon

this rename code

gives this error

Del
zesanchez.JPG
Warning: rename(zesanchez.JPG,Del) [function.rename]: No error in C:\server\xampp\htdocs\images\data2file.php on line 10

Successfully!

and i was thinking if i rename the file i will loss the ext of that file. how do i keep it ?
<?php 



echo $_POST['new-name'];
echo '<br>';
echo $_POST['file-name']; 

rename($_POST['file-name'], $_POST['new-name']);

echo '<br>';
echo "Successfully!"; 
?>

Open in new window

Try this:

<?php 
echo $_POST['new-name'];
echo '<br>';
echo $_POST['file-name']; 

$old = explode($_POST['file-name']);

rename($_POST['file-name'], $_POST['new-name'].".".$old[1]);

echo '<br>';
echo "Successfully!"; 
?>

Open in new window

ok i changed it to this

and it works great thank you
<?php 
echo $_POST['new-name'];
echo '<br>';
echo $_POST['file-name']; 



rename($_POST['file-name'], $_POST['new-name'].".jpg");

echo '<br>';
echo "Successfully!"; 
?>

Open in new window