Unable to transfer a file from a distant location to the server

When attempting to access the remote file as an attachment, I am able to retrieve the headers but not the file or its content.

 <?php
    error_reporting(E_ALL);

    // Various methods tried with no success 
    /*$url = 'http://www.dev-c.com/nativedb/reference.html';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_BINARYTRANSFER, false);
    curl_setopt($curl, CURLOPT_HEADER, true);

    $data = curl_exec($curl);

    curl_close($curl);

    echo $data;*/

    // Similar attempts made here as well
    
    $url = 'http://www.dev-c.com/nativedb/reference.html';
    $file = basename($url);

    $fp = fopen($file, 'w');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);

    $data = curl_exec($ch);

    curl_close($ch);
    fclose($fp);
?>

Despite trying different approaches, none of the codes were successful. The output is empty and the created file on my webserver remains blank. The file in question:

Answer №1

Appreciate the assistance, resolved the issue by adjusting the encoding. Added the "Accept-Encoding: gzip" header and decoded the output using gzdecode.

<?php
error_reporting(E_ALL);

$url = 'http://www.example.com/api/data';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Host: www.example.com",
"Accept-Encoding: gzip"
));

$data = curl_exec($curl);

curl_close($curl);

echo gzdecode($data);
?>

Answer №2

After analyzing the headers of the web URL provided in your example, here is the information retrieved:


HTTP/1.1 200 OK
Server: nginx/1.12.0
Date: Fri, 23 Jun 2017 11:34:58 GMT
Content-Type: application/octet-stream
**Content-Length: 0**
Connection: keep-alive
Content-Description: reference.html
Content-Disposition: attachment; filename=reference.html
Content-Transfer-Encoding: binary
Expires: Thu Jun 22 11:34:58 2017 GMT
Cache-Control: must-revalidate
Pragma: public
Set-Cookie: PHPSESSID=f9d4eb515eeabfb461a11d056fdc7b78; path=/
Cache-Control: max-age=3600, private, proxy-revalidate

The key observation here is that Content-Length is set to zero, which could explain why the body content is empty.

If you can access the NGINX server hosting reference.html, consider checking /var/log/nginx/error.log for any relevant logs during attempts to download the file.

A zero Content-Length typically indicates a problem with the server configuration instead of the actual file size available for downloading.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The sprintf function is not displaying correctly in CodeIgniter

I've been attempting to utilize %s in my language file for the user name, but it's not functioning as expected. Instead of displaying the user name, it leaves a blank space on the page. I am confident that my usage of sprintf is correct for CodeI ...

Concealing databases results in the existence of two localhost servers in phpMyAdmin

In my attempt to hide databases in phpMyAdmin, I inserted the following line into my config.inc.php: $cfg['Servers'][$i]['hide_db'] = 'information_schema|performance_schema|mysql|phpmyadmin'; However, upon logging in now, I ...

Is it possible to retrieve the overall size of all emails stored in a user's account using PHP and cPanel?

Is there a way in PHP to determine the amount of storage that email accounts occupy on a cPanel server? If not using PHP, is there another method to achieve this and display it on a website? I have code for calculating disk usage in a directory, but I&ap ...

PHP script for conducting sensitivity analysis

I am working with a PHP application that calculates a final result based on user input in multiple forms. Imagine a scenario where a user enters a value of 5 on the first page, which is passed through _POST to the next page. Then, they enter a value of 2 ...

If a user has been inactive for a specific period, send an email prompt

Is there a way to accomplish the following: Determine when a user last accessed Wordpress. If it has exceeded X amount of time, send an email notification. The main goal is to motivate users to log in and update the website. I came across this plugin t ...

What is the alternative for mysql_fetch_length function in CodeIgniter?

I need to calculate the total number of characters returned by a database query. In my PHP code, I'm currently utilizing mysql_fetch_length. Does CodeIgniter have a comparable function for this purpose? ...

The <th> and <td> elements retrieved from the database do not align properly in their respective index positions

Hey fellow Developers, I'm currently working on a School Management System and I have two tables in the database - one for Subjects and another for storing scores obtained in those subjects, called 'scores'. My goal is to display subject nam ...

Which fonts are accessible on Linux systems?

Currently, I am working on programming a specialized media box that runs a unique version of Linux (tangox 2.6.29.6-18-sigma #2 PREEMPT Tue Dec 20 14:00:43 UTC 2011 mips GNU/Linux) and need to determine the font options available on this system. My main o ...

Inheritance and Method Overriding in PHP Objects

Recently, I have been working on some PHP code within the CodeIgniter framework. I have created my own Model class which extends and overrides the default CodeIgniter model. Below is the implementation: <?php class MY_Model extends CI_Model { publi ...

Locate the data attribute and store it in the database using an ajax request

I am in need of a proper script to insert a data attribute into the database upon clicking using ajax. HTML: <li><a data-cat="random name">Button</a></li> jQuery: $('li a').click(function() { $(this).data('cat&a ...

Tips for using the with() method in combination with table joins

In my database, I have two main tables with the following fields: devices id name created_at updated_at device_reports id device_id location created_at updated_at I currently have a functioning report with various filters that utilize ...

The unexpected end of input error is caused by an Ajax call that returns a SyntaxError

I recently developed a basic notepad application where users can retrieve a specific file (which essentially translates to a row from MySQL) in order to make edits. When the form is submitted directly to the PHP file without preventing the default behavi ...

Struggling to retrieve the JSON information, but encountering no success

Here is the javascript code snippet: $.getJSON("validate_login.php", {username:$("#username").val(), password:$("#password").val()}, function(data){ alert("result: " + data.result); }); And here is the corresponding php code: <?ph ...

What is the best way to store an array of file names in a single column in a database table?

I have a table named t1 with two columns: id and name. I want to insert file names in the name column. <input type="file" name="files[]" multiple></br> <input type="submit" name="submit" value="ADD"> Table: id | name 1 | e1.jpg, e2 ...

What is the most effective way to perform two GET requests in PHP?

Here's the code snippet I am currently working on: $header = [ 'phoneType' => $mobileType ]; \Drupal::logger("cam")->debug('PageNameHere retrieveAPINameHere REQUEST: '.json_encode($head ...

During the click event, two distinct $.ajax requests interfere and cancel each other out

Here's a dilemma I'm facing: On my webpage, I have implemented two different click events. The first one opens a modal displaying a larger image when you click on a thumbnail picture (similar to Instagram on PC - I created an Instagram clone for ...

Looking for assistance with MySQL DISTINCT functionality

I need to extract unique names from a mysql table to use in an autocomplete feature with jQuery UI. However, there are duplicate names in the table and I want to avoid displaying them in the suggestions. Additionally, I also require selecting other colum ...

Checking for related rows in Laravel Fluent or Eloquent - A necessary step for efficient data

I'm currently faced with a basic issue that requires a solid solution. In my application, each post can have zero or multiple comments attached to it. I am grappling with how to use Fluent or Eloquent to specifically fetch posts that have comments ass ...

Submitting a form using jQuery and processing the response

Can a form be submitted using jQuery without utilizing json, ajax, or other methods for handling the result? For example: <form id="loginform"> //some input fields and a submit button. </form> And then with jQuery: $("#loginform").sub ...

The statusText variable for getXMLHTTP object is not found when the status is not equal to

Earlier, I posted about this issue before isolating the problem. Now that I have isolated it, I wanted to repost with a clearer focus on the two functions causing the problem. Whenever I update my State, it triggers the getCity function. The call is being ...