Jump to content
Zaaka72

Image download from server

Recommended Posts

I have a PHP script that locates a photo in a MySQL database and based on the path /filename stored in the database, retrieves the file and downloads it via the browser, the problem is that it saves as a filename that is the url with no extension.

so we pass : dlfile.php?id-10 the script is supposed to get the filename from the stored file and download it with that filename but it downloads the file as 'dlfile?id=10' instead of PIC76534.jpg. where am i going wrong? code below for dlfile.php.

or is there a way to ask the user to select a filename when downloading?  I know the script is crude but have had it for ages.

<?php
require( '../js/db.php' );
$id = $_GET[ 'id' ];
$sql = "SELECT * FROM image_archive WHERE id = " . $id;
$result = mysqli_query( $conn, $sql );
if ( !$result ) {
	echo "ERROR: " . mysqli_error( $conn );
} else {
	$row=mysqli_fetch_assoc($result);
	//path to the picture you're wanting the viewer to download
	$path = "/archimages/".basename($row['pic_OriginalPath']);

	if ( file_exists( $path ) ) {
		//split the extension and name from eachother
		$e = explode( '.', $path );

		//get the name of the file
		$file_name = $e[ 0 ];

		//image extension
		$extension = $e[ 1 ];

		// Send a header saying we'll be displaying a picture
		header( 'Content-type: '.$row['pic_Type'] );

		// Name the file
		header( 'Content-Disposition: attachment; filename="' . $file_name . $extension . '"' );

		// Path to the picture you're wanting the user/viewer to download
		readfile( $file_name . '.' . $extension );
	} else {
		echo 'The requested image does not exist.'.$path.'<hr>';
	}
}
?>

 

Share this post


Link to post
Share on other sites

Browsers are liable to ignore a Content-Disposition header if it suggests a bad filename. Look into whether that's the case for you.

Share this post


Link to post
Share on other sites
16 hours ago, requinix said:

Browsers are liable to ignore a Content-Disposition header if it suggests a bad filename. Look into whether that's the case for you.

Do you have any suggestions how I can fix this?

Share this post


Link to post
Share on other sites
4 minutes ago, Zaaka72 said:

Do you have any suggestions how I can fix this?

Yes: identify what the problem is with the header and adjust your code to make the problem go away.

If you weren't aware, the filename can't have any sort of directory path in it.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.