Jump to content

[SOLVED] display uploaded file


sasori

Recommended Posts

I created an formupload.php and upload.php script

 

#### formupload.php ###
<html>
<body>
<b>UPLOAD YOUR SHIT IN THERE</b>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'] ?>"
method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="500000" />
<input type="file" name="pix" size="60" />
<input type="submit" name="upload" value="upload picture" />
</form>
</body>
</html>

 

##### upload.php ####
<?php
if(!isset($_POST['upload']))
{
  include("formupload.php");
}
else
    {
      if($_FILES['pix']['tmp_name'] == "none")
      {
        echo " file not uploaded";
        include("formupload.php");
        exit();
      }
      if(!preg_match("/image/",$_FILES['pix']['type']))
      {
        echo " it's not a picture ";
        include("formupload.php");
        exit();
      }
      else
      {
        $destination = 'c:\wamp\tmp'."\\".$_FILES['pix']['name'];
        $tmp_file = $_FILES['pix']['tmp_name'];
        move_uploaded_file($tmp_file,$destination);
        echo "successfully uploaded {$_FILES['pix']['name']}
        ({$_FILES['pix']['size']})";
        echo "<form action='upload.php' method='get'>";
        echo "<input type='submit' value='Upload another file'>";
        echo "</form>";
        echo "<br/>";
      }
    }
?>

 

30w0eg2.jpg

2w4iezo.jpg

and the question is how am I gonna display the uploaded photo on the same page?

 

 

Link to comment
https://forums.phpfreaks.com/topic/124250-solved-display-uploaded-file/
Share on other sites

<img src='path_to_pic.jpg' alt='' />

 

how ? let's say another user uploaded a photo and i don't know what the name of the photo was, all i know is the path which is c:\wamp\tmp , so how am i gonna display the photo after upload when i don't even know the name of the file ? (this bothers me)

dont you save the name of the file in a database or something?

 

If not, I would, and if it is only displayed once, you can use this to display it once

 

$_FILES['pix']['name']

 

 

sir, i don't use a database as for the moment.. i don't know how to display the photo

here' i tried this

 

echo "<img src='".$_FILES['pix']['name']."' />";

 

and still it doesn't display ..can you tell me how the proper way to echo the absolute path?

the images are being sent to c:\wamp\tmp folder

<?php
$path = 'c:\wamp\tmp'."\\".$_FILES['pix']['name'];

$extension = substr($path, -3); 
if($extension == "jpg" || $extension == "jpeg"){ 
   header("Content-type: image/jpeg"); 
}elseif($extension == "gif"){ 
   header("Content-type: image/gif"); 
}elseif($extension == "bmp"){
header("Content-type: image/bmp");
}
readfile($path); 
?>

 

echo "<img src='$path' />";

 

try that and see what you get.

i edited my script and inserted your logic code

<?php
if(!isset($_POST['upload']))
{
  include("formupload.php");
}
else
    {
      if($_FILES['pix']['tmp_name'] == "none")
      {
        echo " file not uploaded";
        include("formupload.php");
        exit();
      }
      if(!preg_match("/image/",$_FILES['pix']['type']))
      {
        echo "<b>LOL! it's not a picture</b>";
        include("formupload.php");
        exit();
      }
      else
      {
        $destination = 'c:\wamp\tmp'."\\".$_FILES['pix']['name'];
        $tmp_file = $_FILES['pix']['tmp_name'];
        move_uploaded_file($tmp_file,$destination);
        echo "<b>successfully uploaded {$_FILES['pix']['name']}
        ({$_FILES['pix']['size']})<b>";
        echo "<form action='upload.php' method='get'>";
        echo "<input type='submit' value='Upload another file'>";
        echo "</form>";
        echo "<br/>";
        echo "<br/>";
        $path = 'c:\wamp\tmp'."\\".$_FILES['pix']['name'];
        $extension = substr($path, -3);
        if($extension == "jpg" || $extension == "jpeg")
        {
          header("Content-type: image/jpeg");
        }
        elseif($extension == "gif")
        {
          header("Content-type: image/gif");
        }
        elseif($extension == "bmp")
        {
          header("Content-type: image/bmp");
        }
        elseif($extension == "png")
        {
          header("Content-type: image/png");
        }
        readfile($path);
        echo "<img src='$path'/>";
      }
    }
?>

24qr989.jpg

it got worst sir ???

Put this in a different file call it getimage.php

 

<?php
$path = $_GET['id'];

$extension = substr($path, -3); 
if($extension == "jpg" || $extension == "jpeg"){ 
    header("Content-type: image/jpeg"); 
}elseif($extension == "gif"){ 
    header("Content-type: image/gif"); 
}elseif($extension == "bmp"){
header("Content-type: image/bmp");
}
readfile($path); 
?>

 

then on your page call it like this

 

echo "<img src='getimage.php?id=" . $destination . "' alt='' />";

 

This worked on my server.

any other way sir? i did what you said

        $destination = 'c:\wamp\tmp'."\\".$_FILES['pix']['name'];
        $tmp_file = $_FILES['pix']['tmp_name'];
        move_uploaded_file($tmp_file,$destination);
        echo "<b>successfully uploaded {$_FILES['pix']['name']}
        ({$_FILES['pix']['size']})<b>";
        echo "<form action='upload.php' method='get'>";
        echo "<input type='submit' value='Upload another file'>";
        echo "</form>";
        echo "<br/>";
        echo "<br/>";
        echo "<img src='getimage.php?id". $destination."' alt=''/>";

 

still nothing happened..it just uploads the file and no display at all

 

2hoac2c.jpg

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.