Help with image upload / display code.

I have some code that is currently working fine.

On my main page I have an input form that calls an image upload script that populates a div and Ajax displays the image without reloading the page.

What I would like to do now is have a button on the page that says display the previous image rather than load a new one. This is what i have so far (but it doesn’t work)

Calling HTML/PHP page

[php]

Choose image to upload
<?php //Select previous or no image if (isset($_POST['none'])) { $newimage = "none" ; $_SESSION['newimage']= $newimage;
}
 if (isset($_POST['previous']))
 {
if(isset($_SESSION['newimage']))
	$newimage = $_SESSION['newimage'];
		else
		{
		$newimage="No Image";
		}
  }
  
  
  ?>

[/php]

Ajaximage.php
[php]

<?php session_start(); $session_id='1'; //$session id $path = "timage/"; $newimage = $_SESSION['newimage'] ; //New stuff if (isset($_POST['previous'])) { echo ""; } //end of new stuff $valid_formats = array("jpg", "png", "gif", "bmp"); if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") { $name = $_FILES['photoimg']['name']; $size = $_FILES['photoimg']['size']; if(strlen($name)) { list($txt, $ext) = explode(".", $name); if(in_array($ext,$valid_formats)) { if($size<(2056*1500)) { $tfn = str_replace(" ", "_", $txt); $tfn = substr($tfn, 0, 8); $actual_image_name = time().$tfn.".".$ext; $tmp = $_FILES['photoimg']['tmp_name']; if(move_uploaded_file($tmp, $path.$actual_image_name)) { //mysql_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'"); echo ""; $newimage = $actual_image_name ; $_SESSION['newimage']= $newimage; } else echo "failed"; } else echo "Image file size max 0.5 MB"; } else echo "Invalid file format.."; } else echo "Please select image..!"; exit; } ?>

[/php]

All of the Ajaximage,php code executes instead of just the first bit. The bits that I have added are in the //New stuff comments. The rest of the code works fine when there is a new image to upload but I don’t want to fill the server with loads of identical images.

Any thoughts help / advice most welcome. (I am pretty new at PHP)

Thanks

Colin

you can use mysql to input a date when it was uploaded and then call it when the button is pushed
it would be your basic mysql query to call a date the last one is put in a and make it call the picture according to the date

sounds complicated but really not

[php]
$sql = “SELECT * FROM table WHERE date = (SELECT MAX(date) FROM table) LIMIT 1”;
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
[/php]
that is the mysql query to grab just make a button a put echo “”;to go to the next page or part

and the insert when the picture is uploaded would be

[php]
$date = date(“M d, Y”);
INSERT INTO table (pic_path, date)
VALUES (‘the pic path and name of pic’, ‘$date’);
[/php]
something like that you can remake it as you see fit
try it let me know

or you can go into the folder that the image is uploaded to and get the latest filetime of that file to display as click the button

//php.net filetime()

http://php.net/manual/en/function.filemtime.php

with glob()

http://www.php.net/manual/en/function.glob.php

Thanks for the suggestion BUT I need it to work by calling ajaximage.php so that the image is displayed without refreshing the page. Also 2 or more people may be writing to the database at the same time so the last image in the folder may not be the inputters last image so it needs to use the file name stored in the session.

I will keep playing to see if I can sort it but any other ideas most welcome.

well the mysql would be your best bet then
in the means of when it is uploaded it inserts the date and path as it is uploaded

Sponsor our Newsletter | Privacy Policy | Terms of Service