Reputation: 280
I have uploaded an image to the folder:
/var/www/uploads/img.png
using the relative path
../uploads/img.png
I then load the image with the following code:
$img = copy('../uploads/9', '/tmp/profile_picture');
Which returns true
And my googling got me to this:
<img src="/tmp/profile_picture" alt="profile_picture" />
I have tried the above with and without .png ending. still not working.
I just need to get out 1 picture and display in an image tag, and i always know the exact path and filename of my image.
EDIT
After the first answer in this thread i have tried the following:
$image = '../uploads/9';
$info = getimagesize($image);
// output the image
header("Content-Disposition: filename={$image};");
header("Content-Type: {$info["mime"]}");
header('Content-Transfer-Encoding: binary');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
readfile($image);
And i call the script here:
<img src="profile_picture.php" alt="profile_picture" />
and i get 404.
I've tried various paths for the $image
Any help appreciated.
Upvotes: 2
Views: 2286
Reputation: 858
You can do it this way.
Use the function sys_get_temp_dir() to upload the images directly to the temporary folder.
Then
$path = '/tmp/profile_picture';
$imageData = file_get_contents($path);
// display in view
echo sprintf('<img src="data:image/png;base64,%s" />', base64_encode($imageData));
Upvotes: 0
Reputation: 588
Use:
$img = copy('/tmp/profile_picture', '../uploads/9');
instead.
From the PHP.net docs, the format for the copy function is:
copy ( string $source , string $dest [, resource $context ] )
Where the parameters:
source
» Path to the source file.
dest
» The destination path. If the destination file already exists, it will be overwritten.
context
» A valid context resource created with stream_context_create().
You'll need to know your web root.
Consider this folder structure:
/var/www
» your www root
/var/www/project1
» your www root
/var/www/project1/assets/img
» your publicly accessible images directory for your project
then upload your image to the above path: /var/www/project1/assets/img/img.png
and use the image like this (from index.html
):
<img src="/assets/img/img.png" alt="Image">
This requires a custom php file, and it's use in a image tag.
Example:
image.php
<?php
$image = '/var/www/uploads/img.png';
$info = getimagesize($image);
// output the image
header("Content-Disposition: filename={$image};");
header("Content-Type: {$info["mime"]}");
header('Content-Transfer-Encoding: binary');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
readfile($image);
?>
index.html
<img src="image.php" alt="Image">
Explanation
This option just wraps your image in php script and the script is called instead.
I use this if the image cannot be publicly accessible and/or I have to do some checks to see if the current user is worthy of accessing the image.
::Used by most file sharing web-apps
Upvotes: 1