php image jpg header problem - php

HI,
i have an image .jpg into my server. If I link direct to image i have this error:
Forbidden
You don't have permission to access http://...
I need to show with php. I have try
header('Content-Type: image/jpeg');
readfile('$file');
But nothing...
I have also try using server root...
any suggestions?

You are trying to load a file called $file. That's a weird name for a picture; I assume it's a PHP variable name where you store the picture file name. In such case:
header('Content-Type: image/jpeg');
readfile($file);
exit;

'$file'
in single quotes is looking for a file called $file
"$file"
in double quotes
or even without any quotes at all, is looking for a file called by the value stored in $file

You have to set the correct rights for the image. You can do this via ssh (console) or easier with your ftp program. If done so, you can access it directly

Probably the file is in a directory that the web server isn't set up to allow access to. PHP can access pretty much any directory on the web server, but apache/IIS/etc will restrict normal access by default to only directories specified in their configuration. If this is the problem then serverfault may have better expertise to help you get your server set up.

Related

Concerns on file permissions

I have a video file and I have commanded the server through htaccess to redirect when requesting the file url. However, I wonder if someone remote can use php functions such as file_get_contents to access the video file since I have only one server and I am not sure whether remote servers can access to it. In my own server I can access to it. Yet I don't want others to access the video file unless authenticated by php.
If you really do not want people to get that video why have it in public web folder? Just put it somewhere else and problem solved. What is the use case of this?
That said, if your redirect is working correctly, the file will not be served. file_get_contents() is still requesting the file from your webserver so it can't just magically ignore the redirect.
If you want to be able to download that file but prevent everyone else from doing it, put the file out of your www root and have a php script to retreive it. You can set up basic http authentication to prevent anyone accessing the php script.
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary")
header("Content-disposition: attachment; filename=\"myvideo.avi\"");
echo file_get_contents("/directory/out/of/www/that/is/readable/by/www-data");

file_put_contents could not store image on server

$path = 'http://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc6/372096_100002534902767_1927052265_n.jpg';
$info = file_put_contents('new/angel.jpg', file_get_contents(urldecode($path)));
echo $info;
It works fine on localhost but it did not work on my website.
Any idea what the problem might be?
Check your logs for error messages
Does the folder ("new") exist?
Are permissions set to allow writing by scripts?
Are you sure the error is in file_put_contents? file_get_contents could fail if the host has disallowed url_fopen.
unless $path is actually hardcoded, you will probably introduce an arbitrary file disclosure security issue. Make sure you validate your input.
you need to give a permission (write ) to upload folder
you can do that by the FTP program by using this codes in numeric value :777
Make sure that there are write permissions to the file in which you are writing
I think the relative path is the problem. It can possible that your local has the path to that file but the path does not exists on server.
3.Please put a forward slash in line $info=file_put_contents('new/angel.jpg', file_get_contents(urldecode($path))); before your "new" folder and try i think this might be a problem

accessing file of another drive

previously I could download wav file in php and IIS. But now file is not downloadable.I don't know what is going wrong . After installing and changeing php 5.3 to php 5.4 with php manager in IIS, the file is not able to downloaded. I have link to file to download it which looks like this:
Download
download.php scripts
<?php
$filename = $_GET['voice'];
$dir = 'd:/temp_file/voice/';
if(is_file($dir.$filename))
{
header('Content-type: audio/wav');
header('Content-Disposition: attachment; filename='".$filename.'"');
echo file_get_contents($dir.$filename);
}
?>
while I was trying to find the mistake why file is not downloadable, I remove if statement and run program, it does prompt window download file with audio player of empty wav file. So, I conclude that there is mistake in path which is not allowing to access file of another drive. I have php code in c:\inetpub\wwwroot\ but wav file to be download is in the d:\temp_file\voice path. What Should I have to do?
Maybe PHP open_basedir restriction.
Try to add d:/temp_file/voice/, to its content in php.ini.
Update
I never used IIS, but another thing that you have to check is if the IIS User have permission to read that directory. Try to add, just for test, Everyone with all permission to d:\temp_file\voice.
I don't know what is going wrong
Then enable error reporting, for what it looks like to me it's a syntax error at header('Content-Disposition: attachment; filename='".$filename.'"'), you're missing a quote (') after filename='". But since you say it has worked (and works without the if), I'd say that's a copy paste error.
Then do a var_dump($filename), which might contain quotes, since you put those around the URL in the link (<a href="download.php?voice='$filename'">). The file D:/temp_file/voice/'foo.wav' might not exist.

PHP output internal server file?

On my server I have a directory with music files, generally in .mp3 format. I'm writing a web application to search for and play these tracks.
All the files are stored, with their absolute server path, artist, album and title info in a MySQL database.
What I want to do is have a PHP file that "outputs" an mp3 file on the server that would normally be inaccessible from the web. So, this is what I want to achieve:
client requests play.php?id=10
PHP gets absolute server path from MySQL database where ID = 10
PHP outputs the file (which would really be at e.g. '/home/user/files/no_web/mp3/Thing.mp3')
To the client, it looks like there is an mp3 file called http://myserver.com/play.php?id=10 and it starts to play.
I'm sure this sort of thing is possible, just not sure how. Thanks in advance :)
You need to send correct content-type header and then just output the file:
header('Content-type: audio/mpeg3');
readfile('filename.mp3');
For reading the file and sending it, you can use the readfile function.
For setting the mime-type, so the browser actually knows what type of file is sent by the webserver, use the header function like:
header('Content-Type: audio/mpeg');
Additionally, you may also want to set the Content-Length HTTP header.
header('Content-Length: ' . filesize($filepath) );
If all you're trying to do is let the user download the mp3, just use the readfile command which will read the mp3 file and pass it along to the client. However you need to make sure to set the mime-type correctly.

PHP: prompting download from ftp?

hy guys,
i really need your help. i've succesfully connected to ftp server via php.
i'm listing all files that are on the server. if i click a file the browser should prompt a download window to download the file.
i've absolutely no idea how to do that. which method am i going to use. ftp_get kind of confuses me. it says i have to declare a local_file as well. i just want a file on the server to download to my harddrive.
how can i do that?
regards matt
The remote file has to first be downloaded to your server before you can send it to the user. It's invisible to the user, but you don't have a choice. PHP won't let the browser talk directly to the FTP server.
Create a separate php script that calls ftp_get for a specific file, stores it temporarily to your server to allow the user to download it.
Something like:
<?php
//assume the page was called like download.php?filename=downloaded.pdf
header('Content-Disposition: attachment; filename="'.$_GET['filename'].'"');
$tempFile = 'temp'.rand();
ftp_get($ftp, $tempFile, $_GET['filename'], FTP_BINARY);
readfile($tempFile);
You may add code to delete the tempFile too.
If you provide a link to a file that can't be read by the browser (such as a php file, audio, video, etc.) it will ask you to download the file.
The other way is to use PHP headers on a page and print out the page, and link to that page. http://www.ryboe.com/tutorials/php-headers-force-download

Categories