Jump to content

Display images after image Upload


AmericanAlien

Recommended Posts

The header() function cannot be used after output has been sent to browser.  This includes any HTML outside of PHP, and whitespace outside of PHP, or any echo, print, print_r, etc. (any function or construct that outputs to the page, including errors, notices, etc.).

Link to comment
Share on other sites

The header() function cannot be used after output has been sent to browser.  This includes any HTML outside of PHP, and whitespace outside of PHP, or any echo, print, print_r, etc. (any function or construct that outputs to the page, including errors, notices, etc.).

 

header("Location:display.php?imageName=".$file_name ); exit();

 

So this line is not needed? However, why is the display.php not showing up? I am not following that. So, if I get rid of that header, what route do I use to get location?

Link to comment
Share on other sites

The header() function cannot be used after output has been sent to browser.  This includes any HTML outside of PHP, and whitespace outside of PHP, or any echo, print, print_r, etc. (any function or construct that outputs to the page, including errors, notices, etc.).

 

Ok, from what I am reading, I do need the header, unless I am interpreting it incorrectly. How would I achieve this type of result?

Link to comment
Share on other sites

Following code is the path of image into the variable and simply displaying the image from that path. If you know the path where the image is being saved temporarily on your server then you can easily display it by using the following code but make sure your image is getting saved first..

<img src = "<?php echo $pic; ?>" width="100" height="100" />

Link to comment
Share on other sites

Following code is the path of image into the variable and simply displaying the image from that path. If you know the path where the image is being saved temporarily on your server then you can easily display it by using the following code but make sure your image is getting saved first..

<img src = "<?php echo $pic; ?>" width="100" height="100" />

 

Thanks for the help! I had to go in another direction for now. A quick question, how is that code displaying the pic in a new page? I see it sources the pic, but I do not see how it takes the pic and places it into an html or even php page in a type of gallery.

Link to comment
Share on other sites

FINAL WORKING CODE EXAMPLE OF UPLOADING IMAGES AND DISPLAYING THEM BY PUTTING SOME REQUIRED RESTRICTIONS ON THE UPLOADED FILE:

NOTE: BEFORE USING MY CODE CREATE ThE FOLDER named as 'upload' from where you are running my code.

 

Upload form.php

<html>
<body>

<form action="upload-restrict.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

 

upload-restrict.php

 

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg"))
&& ($_FILES["file"]["size"] < 2000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
 // echo '<img src = . "upload/" . $_FILES["file"]["name"] />';
 echo "<br />";
 echo "<br />";
   echo "<img src='upload/" . $_FILES["file"]["name"]. "'>" ; // File is getting displayed from here
      }
    }
  }
else
  {
  if (($_FILES["file"]["type"] != "image/jpeg"))
  echo "Invalid file TYPE";
  
   if (($_FILES["file"]["size"] < 20000))
  echo "\nInvalid file size";
  
  else
  {
    echo "Invalid file unknwon reason";
  }
  }
?>

 

The answer of your question if you mean the sources "PATH of the image like upload/picname.jpeg" All you need to do is assign that path to $pic and <img src = "<?php echo $pic; ?>" width="100" height="100" /> will display the image on the page.

 

NOTE: BEFORE USING MY CODE CREATE ThE FOLDER named as 'upload' from where you are running my code.

Link to comment
Share on other sites

FINAL WORKING CODE EXAMPLE OF UPLOADING IMAGES AND DISPLAYING THEM BY PUTTING SOME REQUIRED RESTRICTIONS ON THE UPLOADED FILE:

NOTE: BEFORE USING MY CODE CREATE ThE FOLDER named as 'upload' from where you are running my code.

 

Upload form.php

<html>
<body>

<form action="upload-restrict.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

 

upload-restrict.php

 

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg"))
&& ($_FILES["file"]["size"] < 2000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
 // echo '<img src = . "upload/" . $_FILES["file"]["name"] />';
 echo "<br />";
 echo "<br />";
   echo "<img src='upload/" . $_FILES["file"]["name"]. "'>" ; // File is getting displayed from here
      }
    }
  }
else
  {
  if (($_FILES["file"]["type"] != "image/jpeg"))
  echo "Invalid file TYPE";
  
   if (($_FILES["file"]["size"] < 20000))
  echo "\nInvalid file size";
  
  else
  {
    echo "Invalid file unknwon reason";
  }
  }
?>

 

The answer of your question if you mean the sources "PATH of the image like upload/picname.jpeg" All you need to do is assign that path to $pic and <img src = "<?php echo $pic; ?>" width="100" height="100" /> will display the image on the page.

 

NOTE: BEFORE USING MY CODE CREATE ThE FOLDER named as 'upload' from where you are running my code.

 

The code is working AWESOMELY! The actually shows up this time. I appreciate your hard work. One thing. This is a permanent page, the pics do not stay in that page so others can view it. Is this not possible? What I mean is, is there a way to have each picture that is upload to stay on the webpage in the order uploaded and others can view this page as well?

Link to comment
Share on other sites

Well it is possible.

I am just giving you a suggestion

If you would like to display images to a gallery that are being uploaded then all you have to do is to use loops in order to display all the images from certain directory into the gallery dynamically but this thing can very very very easily done if you use databases .

For more help i recommend you to watch video tutorial step by step coding for adding and displaying an image from certain directory

http://www.youtube.com/watch?v=dHq1MNnhSzU&feature=youtube_gdata

 

A complete tutorial step by step of how to make an Image Upload Website, MANAGE GALLERY ETC

http://www.youtube.com/watch?v=geoF9fqQbKI&feature=youtube_gdata

 

I hope your issue will gonna resolve.

Link to comment
Share on other sites

Well it is possible.

I am just giving you a suggestion

If you would like to display images to a gallery that are being uploaded then all you have to do is to use loops in order to display all the images from certain directory into the gallery dynamically but this thing can very very very easily done if you use databases .

For more help i recommend you to watch video tutorial step by step coding for adding and displaying an image from certain directory

http://www.youtube.com/watch?v=dHq1MNnhSzU&feature=youtube_gdata

 

A complete tutorial step by step of how to make an Image Upload Website, MANAGE GALLERY ETC

http://www.youtube.com/watch?v=geoF9fqQbKI&feature=youtube_gdata

 

I hope your issue will gonna resolve.

 

Trust me, you have helped me a ton. I understand the loop route you speak of. I have tried that from the start, but maybe my paths are not inputted correctly. Maybe I can do what I do with my forms and have the info (pics)inputted into a form type php document. I think that may work, I have not tried that yet. Your code was the first code that actually worked, and I think I can use that and use my form method and integrate them or mesh them together. At least I hope so.

 

Again, thanks for your tremendous help, absolutely appreciated! 

Link to comment
Share on other sites

  • 1 year later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.