Download the Contents of a Url in PHP Even If It Returns a 404

Download the contents of a URL in PHP even if it returns a 404

By default file_get_contents only returns the content of HTTP 200 responses.

With curl you get the headers and the content separately.

As of PHP 5.0, you can also specify a context for file_get_contents, allowing you to do it without relying on url (See Gordon's answer).

PHP file_get_contents and others return 404 but URL is accessible via browser

So what I ended up doing was using wget since most other methods were not working. Below is the working function.

     /**
* @param string $path
* Checks to see if a file exists and is readable then if it is, downloads it.
*/
public function downloadFile($path){
echo "Grabbing File:" .$path."\n";
shell_exec("wget -P".$this->getDirectory()." ".$path);
//echo "Attempting to place ".basename($path)." in ".$this->getDirectory();
}

I never really found out why it would return a 404 to a publically accessible URL. But that was the work around I came up with. You can view the whole file here.

Directory Download Link not working Server Error 404 - File or directory not found.

I fixed it.

Change this line:

print "<td><a href=pdf_download.php?filename={$path_noext}>{$file['title']}</a></td>\n";

To this:

print "<td><a href=docs/{$file['title']}>{$file['title']}</a></td>\n";

It couldn't find the files because the download link was pointed to the root directory not to the documents directory.

Easy way to test a URL for 404 in PHP?

If you are using PHP's curl bindings, you can check the error code using curl_getinfo as such:

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);

/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);

/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
/* Handle 404 here. */
}

curl_close($handle);

/* Handle $response here. */

URL Says 404 Not Found, Yet File Downloads

Here are the headers that I get from the image in your post:

HTTP/1.1 200 OK
Expires: Sun, 07 Apr 2023 04:18:16 GMT
Server: IceWarp/10.4.4
Date: Sun, 07 Apr 2013 04:18:16 GMT
Content-Type: application/octet-stream
Content-Length: 9797
Last-Modified: Sun, 07 Apr 2013 03:47:12 GMT

No 404, but the file's content type is set to octet-stream (a stream of 8 bits), which is used when you want someone to download a file.

You need to tell your server company to configure the server so that it serves images with their proper Content-Type headers.

How to fix Error 404 when trying to download PDF file from the public folder

This works directly without Route or any controller.

<tbody>
@foreach ($allData as $key => $patient)
<tr>
<td>{{ $patient -> card_number }}</td>
<td>{{ $patient -> patient_type }}</td>
<td>{{ $patient -> patient_hmo }}</td>
<td>{{ $patient -> patient_firstname }} {{ $patient -> patient_lastname }}</td>
<td>{{\Carbon\Carbon::parse($patient -> patient_dob)->diff(\Carbon\Carbon::now())->format('%y') }}</td>
<td>{{ $patient -> patient_phone }}</td>
<td>{{ $patient -> patient_gender }}</td>
<td>{{ $patient -> patient_kin_name }}</td>
<td>{{ $patient -> patient_kin_phone }}</td>

<td><a href="{{ url('/'.$patient->file) }}">Download</a></td>
<td>

<a class="btn btn-primary btn-rounded btn-sm" title="View Data" data-toggle="modal" data-target="#modal-center{{ $patient->id }}"> <i class="fa fa-eye-slash"></i></a>

<a href="{{ route('patient.edit',$patient->id) }}" class="btn btn-info btn-rounded btn-sm" title="Edit Data" > <i class="fa fa-edit"></i></a>

<a href="{{ route('patient.delete',$patient->id) }}" class="btn btn-danger btn-rounded btn-sm" id="delete" title="Delete Data" > <i class="fa fa-trash"></i></a>
</td>

</tr>
@endforeach

</tbody>

How do get the URL of a Not Found page, in order to display it in a custom 404 page?

I found the Issue, You must post a relative path, not a direct link, otherwise it will redirect to that page instead of showing the 404.

Eg:

 ErrorDocument 404 /404.php

That way the .htacess doesn't redirect to Yoursite.com/notfound.php
It will also send a 404 (Not Found) instead of a 200 (OK)

How can I handle the warning of file_get_contents() function in PHP?

Step 1: check the return code: if($content === FALSE) { // handle error here... }

Step 2: suppress the warning by putting an error control operator (i.e. @) in front of the call to file_get_contents():
$content = @file_get_contents($site);



Related Topics



Leave a reply



Submit