开发者

How to get a link from an uploaded file?

开发者 https://www.devze.com 2023-02-01 22:22 出处:网络
I use this simple script to build a simple image upload form - http://www.w3schools.com/PHP/php_file_upload.asp

I use this simple script to build a simple image upload form - http://www.w3schools.com/PHP/php_file_upload.asp

The script is working 开发者_运维百科well and the image is stored in a folder called 'upload'.

The question is, how to echo back the uploaded file in my php script so that I can display the uploaded image in my website.

Thanks for helping! :D


The last script in your linked code (section entitled Saving the Uploaded File) states where it has been saved:

echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

So, assuming upload is right below the webroot, your src would be:

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


Typically, depending on your server config, the image (or any upload) is uploaded in /tmp. You'll need to MOVE the copied file to your web directory (possibly in a subdirectory like "uploaded images". You can rename the file in this process as well, because it's name will be adlkfa23ur20dasjsdablah.jpg


The uploaded file will be deleted if you do not copy it so. So copy it to either public or private directory, as in link w3school...:

 move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

Public, point to the file as in

<img src="path-to-img.jpg" />

and then the file needs to reside in a readable directory by the webserver. The default fileupload-directory is not readable, so copy the file.

Private, you use php to get the file for you:

<img src="getFile.php?filename=myfile.jpg">

//getFile.php
header('content-type: image/jpeg');
echo file_get_contents('myjpg.jpg');

regards, /t

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号