Image Upload -> Open file directly

Heya.

I’m trying my luck on a simple file uploader and grabbed most of the parts I need from other scripts, so… I’m not really sure what I am doing :S

Well, I’m satisfied with my results so far, but there’s one thing that bugs me: When the uploading is done, I only get a few lines of text in a new tab, but I want the picture to pop up in a new tab instead of forcing me to search for it afterwards.

I’m sure it’s possible, but… yeah, I don’t know how.

My script:

[code=“The upload-form”]

[/code]

[code=“upload_file.php”]<?php
$allowedExts = array(“jpg”, “jpeg”, “gif”, “png”);
$extension = end(explode(".", $_FILES[“file”][“name”]));
if ((($_FILES[“file”][“type”] == “image/gif”)
|| ($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “image/png”)
|| ($_FILES[“file”][“type”] == “image/pjpeg”))
&& ($_FILES[“file”][“size”] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES[“file”][“error”] > 0)
{
echo "Return Code: " . $_FILES[“file”][“error”] . “
”;
}
else
{
echo "Upload: " . $_FILES[“file”][“name”] . “
”;
echo "Type: " . $_FILES[“file”][“type”] . “
”;
echo “Size: " . ($_FILES[“file”][“size”] / 1024) . " Kb
”;
echo "Temp file: " . $_FILES[“file”][“tmp_name”] . “
”;

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: " . "http://website.net/folder/upload/" . $_FILES["file"]["name"];
  }
}

}
else
{
echo “Invalid file”;
}
?>[/code]

And in the end, it looks like this:

So… how can I change that? My attempts only resulted in syntax-errors because I’m guessing around like hell D:

Have a look at this script, it is one I use and it works well.

Well, now smashing another script in my pile of fragments is not really something I aim for D:
Thank you, but I’d rather know how I can edit my existing script D:

I must be stupid because I can’t find an edit-button :c

Anyway, after some thoughts:
I don’t want the image to pop up in a new tab.

Instead, it should be shown in the text-thingy.
Something like

Upload: step1.png Type: image/png Size: 16.314453125 Kb Temp file: /users/gengah/temp/phpvv27Y7 Stored in: http://website.net/folder/upload/step1.png

I tried that a few times in different variations, but I get syntax-errors every time… I just can’t get past these variables-thing. Would somebody mind explaining it? D:

Hi Creeper,

If you just want to display the uploaded image below your text, adding this line should do it:[php]echo ‘<img src="upload/’.$_FILES[“file”][“name”].’" alt=“uploaded image”>’;[/php]

You should add it immediately below the echo "Stored in: line.

The final result would be:[php]<?php
$allowedExts = array(“jpg”, “jpeg”, “gif”, “png”);
$extension = end(explode(".", $_FILES[“file”][“name”]));
if ((($_FILES[“file”][“type”] == “image/gif”)
|| ($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “image/png”)
|| ($_FILES[“file”][“type”] == “image/pjpeg”))
&& ($_FILES[“file”][“size”] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES[“file”][“error”] > 0)
{
echo "Return Code: " . $_FILES[“file”][“error”] . “
”;
}
else
{
echo "Upload: " . $_FILES[“file”][“name”] . “
”;
echo "Type: " . $_FILES[“file”][“type”] . “
”;
echo “Size: " . ($_FILES[“file”][“size”] / 1024) . " Kb
”;
echo "Temp file: " . $_FILES[“file”][“tmp_name”] . “
”;

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: " . "http://website.net/folder/upload/" . $_FILES["file"]["name"];
  echo '<img src="upload/'.$_FILES["file"]["name"].'" alt="uploaded image">';
  }
}

}
else
{
echo “Invalid file”;
}
?>[/php]

Thank you, malasho. I found a similar solution.

[php] echo “”;[/php]

The confusing thing for me was that in php the images use ’ instead of ". After realizing that it was only a matter of time. I set " everywhere, and then the idea with “’>” came up.
Well, it works now.

The only thing left: Changing it so the image appears in the text instead of a new tab ^^

Glad you got it working. I’m not quite sure what you mean about the image appearing in a new tab. I’d be happy to help you get it working the way you want if you could clarify that for me.

Also, just for your knowledge: You can easily use single quotes or double quotes with images in php.

Here are five different ways that you could write this: (all should work for you)[php]echo ‘<img src="upload/’,$_FILES[“file”][“name”],’" alt=“uploaded image”>’;
echo “<img src='upload/”,$_FILES[‘file’][‘name’],"’ alt=‘uploaded image’>";
echo ‘<img src="upload/’.$_FILES[“file”][“name”].’" alt=“uploaded image”>’;
echo “<img src='upload/”.$_FILES[‘file’][‘name’]."’ alt=‘uploaded image’>";
echo “<img src=‘upload/{$_FILES[‘file’][‘name’]}’ alt=‘uploaded image’>”;[/php]

You will note that in the above examples, you can use both commas and periods. In this case, commas are more efficient and offer a slight advantage. The commas work here because they are separating elements in an echo. If you were assigning the image element to a variable, you would have to use the periods.

You will also note that each uses the alt attribute. The alt attribute is designated as required and should be used. Chances are things will display fine without it, but it should be included.

Let me know if you’d like some help with getting the image to appear the way you would like.

jay

Thanks again, but I think I’m done now.

Regarding the “image in a new tab”: I meant a php-iframe-workaround, but I have 3 pages now (including one info-page), and it works fine, so… hey, why change it? :smiley:

And as far as it applies to me, I’ll stay with [i]Everything that’s a double quote in HTML becomes a single quote in PHP. I wonder how far it gets me :S

Anyway, thank you for helping me out :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service