1.

How to download files from an external server with code in PHP?

Answer»

We can do it by various methods, If you have allow_url_fopen set to true:

  • We can download images or files from an external server  with cURL() But in this case, curl has been enabled on both servers
  • We can also do it by file_put_contents()
Example

$ch = curl_init();
$source = "http://abc.com/logo.jpg";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "/images/newlogo.jpg";
$file = fopen($destination, "w+");
FPUTS($file, $data);
fclose($file);

/*-------This is another way to do it-------*/

$url = 'http://abc.com/logo.jpg';
$img = '/images/flower.jpg';
file_put_contents($img, file_get_contents($url));

18. How many types of errors in PHP?

Primarily PHP supports four types of errors, listed below:-

1. Syntax Error: It will occur if there is a syntax MISTAKE in the script

2. Fatal Error: It will happen if PHP does not understand what you have written. For example, you are calling a function, but that function does not exist. Fatal errors stop the execution of the script.

3. Warning Error: It will occur in FOLLOWING cases to include a missing file or using the incorrect number of parameters in a function.

4. Notice Error: It will happen when we try to access the undefined variable.

Note: The page you are accessing has some of the most basic and complex PHP Interview Questions and Answers. You can download it as a PDF to read it later offline.



Discussion

No Comment Found