Solved

Display and rename that image in php

Posted on 2011-03-04
8
413 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:mikearmas
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:Duboux
ID: 35041565
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...
}
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35042068
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>
}
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 35042081
Try with this loop for the main page:

<?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"<form method=\"post\" action=\"data2file.php\">";
                   echo '<a href="edit.php" ><img border=1 src="'.$file.'"></a>'; 
                   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 "<input type=submit name=\"$file\" value=Edit>" ; 
                   print $file; 
                   echo '<br>'; 
                   echo '<hr>'; 
                   echo "</form>";
            
            } 
        } 
echo '</td>'; 

echo ' 

</table>'; 
 
?> 

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35042083
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Closing Comment

by:mikearmas
ID: 35043689
Thank you that works great
0
 

Author Comment

by:mikearmas
ID: 35043739
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

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35044268
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

0
 

Author Comment

by:mikearmas
ID: 35044781
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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now