2

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.

3
  • 1
    Not working means what Commented Jan 4, 2016 at 19:54
  • <img src="uploads/img.png"/> why do you need to copy it? Commented Jan 4, 2016 at 19:56
  • Why don't you use <img src="/uploads/img.png" alt="profile_picture" />? If you start with / on a website, it means the web server root, not your local file system root. Commented Jan 4, 2016 at 19:57

2 Answers 2

1

Step 1: correct your use of copy()

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().


Step 2 (1): upload image to a web accessible path

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">


Step 2 (2): OR wrap your image in a php file

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

Sign up to request clarification or add additional context in comments.

2 Comments

I have copied your script and it returns 404, i have tried with the php script residing in the same folter as the view
And the first part of your answer is obsolete and i think you misunderstood. Source is uploads/9 and destination is /temp/9
0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.