Solved

Display and rename that image in php

Posted on 2011-03-04
8
411 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Closing Comment

by:mikearmas
Comment Utility
Thank you that works great
0
 

Author Comment

by:mikearmas
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
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.

762 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

11 Experts available now in Live!

Get 1:1 Help Now