[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Display and rename that image in php

Posted on 2011-03-04
8
Medium Priority
?
421 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 2000 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
Technology Partners: 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!

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

656 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