Solved

Display and rename that image in php

Posted on 2011-03-04
8
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

688 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