Zaaka72 Posted May 18, 2020 Share Posted May 18, 2020 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>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/310805-image-download-from-server/ Share on other sites More sharing options...
requinix Posted May 18, 2020 Share Posted May 18, 2020 Browsers are liable to ignore a Content-Disposition header if it suggests a bad filename. Look into whether that's the case for you. Quote Link to comment https://forums.phpfreaks.com/topic/310805-image-download-from-server/#findComment-1578124 Share on other sites More sharing options...
Zaaka72 Posted May 18, 2020 Author Share Posted May 18, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/310805-image-download-from-server/#findComment-1578167 Share on other sites More sharing options...
requinix Posted May 18, 2020 Share Posted May 18, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310805-image-download-from-server/#findComment-1578168 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.