Php-mysql Course

The code presented in this page can be used to save / copy images on server from external URL address.

- Important: If you work on Linux system, PHP must have CHMOD write permisions in the directory in which the image file will be saved.
<?php
// function to get content from a URL address, with cURL
// returns the content of the page with the address pased to $url parameter
function getContentUrl($url) {
 // https://coursesweb.net/php-mysql/
  // Seting options for cURL
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/21.0 (compatible; MSIE 8.01; Windows NT 5.0)');
  curl_setopt($ch, CURLOPT_TIMEOUT, 200);
  curl_setopt($ch, CURLOPT_AUTOREFERER, false);
  curl_setopt($ch, CURLOPT_REFERER, 'http://google.com');
  curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);    // Follows redirect responses

  // gets the file content, trigger error if false
  $file = curl_exec($ch);
  if($file === false) trigger_error(curl_error($ch));

  curl_close ($ch);
  return $file;
}

$src = 'https://marplo.net/imgs/webcourses.gif';       // Image address
$dirimg = 'imgs/';            // directory in which the image will be saved
$localfile = $dirimg. basename($src);         // set image name the same as the file name of the source

// create the file with the image on the server
file_put_contents($localfile, getContentUrl($src));

// Test, show the saved image
echo '<img src="'. $localfile .'" />';
?>
If the image file already exists on server, in the directory from $dirimg, the file will be replaced with the new image.
If you want to not replace the old file, but to save the image with another name, replace this line:
file_put_contents($localfile, getContentUrl($src));
With this code:
// receives the file address, passed by refference
function setLocalfile(&$localfile) {
  GLOBAL $dirimg;
  $imgnm = basename($localfile);      // gets the file name
  preg_match('/^[0-9]*/i', $imgnm, $nrimg);      // gets the number from beginning of file name
  if($nrimg[0] === '') $imgnm = '0_'. $imgnm;   // if no number, sets 0
  else $imgnm = str_replace($nrimg[0], $nrimg[0] + 1, $imgnm);       // replace the number with next one
  $localfile = $dirimg. $imgnm;        // sets the new name, passed by refference to $localfile
  if(file_exists($dirimg. $imgnm)) setLocalfile($localfile);        // calls the function to set another name
}

// if the file exists in $dirimg, calls setLocalfile() to set a new file name
if(file_exists($localfile)) setLocalfile($localfile);

// create the file with the image on the server
file_put_contents($localfile, getContentUrl($src));
- This code, the setLocalfile() function sets a new name with a consecutive number at the beginning of the file name (image.png, 0_image.png, 1_image.png, ...).

- The getContentUrl() can also be used to get the page content of an URL address.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is a block element?
<div> <img> <span>
<div>Web Programming and Development</div>
Which CSS code displays the text underlined?
font-style: italic; text-decoration: underline; font-weight: 500;
h2 {
  text-decoration: underline;
}
Click on the JavaScript function that can access other function after a specified time.
insertBefore() setTimeout() querySelector()
function someFunction() { alert("CoursesWeb.net"); }
setTimeout("someFunction()", 2000);
Click on the instruction that returns the number of items of a multidimensional array in PHP.
count($array) count($array, 1) strlen()
$food =["fruits" =>["banana", "apple"), "veggie" =>["collard", "pea"));
$nr_food = count($food, 1);
echo $nr_food;       // 6
Save image on server from external URL

Last accessed pages

  1. Calling Function and Class Method with Name from String (5747)
  2. PHP MySQL - DELETE (1989)
  3. Split number - Get each character from number (936)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (137643)
  5. Get Mime Type of file or string content in PHP (6079)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (332)
  2. Read Excel file data in PHP - PhpExcelReader (127)
  3. The Four Agreements (98)
  4. PHP Unzipper - Extract Zip, Rar Archives (95)
  5. The Mastery of Love (90)
Chat
Chat or leave a message for the other users
Full screenInchide