Introductory Tutorial: https://youtu.be/pWG7ajC_OVo?list=PL4cUxeGkcC9gksOX3Kd9KPo-O68ncT05o

necessary command to integrate apache2 with php7.0

mpm is necessary.

sudo apt-get install php7.0
sudo apt-get install php7.0-fpm
sudo apt-get install php-cli
sudo apt-get install php-curl
sudo a2enmod php7.0
sudo a2dismod mpm_event
sudo a2enmod mpm_prefork
sudo service apache2 restart

 

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

An html form that can upload a file to the server:uploadfile

<!DOCTYPE html>
<! uploadfile.html used on the client browser>

<html>

<head>
<title>Upload your files</title>
</head>

<body>

<form enctype=”multipart/form-data” action=”AmirFileGetter.php” method=”POST”>

<p>Upload your file</p>

<!–  this will open a dialog –>
<input type=”file” name=”uploaded_file”></input><br />

<p><input type=”submit” value=”Submit” id=”submit-id”/></p>

</form>

</body>

</html>

———————–=————————————

the php on the server to receive the uploaded file:AmirFileGetter

#AmirFileGetter.php  in the var/www/html folder in apache2

<?php
echo ‘Current script owner: ‘ . get_current_user();
echo “<p></P>”;
#the upload destination can be anywhere must be permissioned twice and  the first one is necessary

#sudo chmod -R 777 /home/amir/Desktop/uploadedfiles

$uploads_dir = ‘/home/amir/Desktop/uploadedfiles/’;
echo “uploaded_file in this script must be exactly the same name in the file input tag in the form”;
echo $_FILES[“uploaded_file”][“name”];
echo “<p></P>”;
echo $_FILES[“uploaded_file”][“error”];
echo “<p></P>”;
echo $_FILES[“uploaded_file”][“tmp_name”];

$tmp_name =”/tmp/” . basename($_FILES[“uploaded_file”][“tmp_name”]);
echo “<p></P>”;
echo $tmp_name;
$name = basename($_FILES[“uploaded_file”][“name”]);
$uploadfile=$uploads_dir . “amir” . $name ;
echo “<p></P>”;
echo $uploadfile;
#this is a php command
echo “<p></P>”;
if (file_exists($tmp_name)) {
echo “The file $tmp_name exists”;
} else {
echo “The file $tmp_name does not exist”;
}
if (move_uploaded_file($tmp_name,$uploadfile )) {
echo “File is valid, and was successfully uploaded.\n”;

if (file_exists($uploadfile)) {
echo “The file $uploadfile is safe and sound”;
} else {
echo “The file $uploadfile is not actually uploaded”;
}
} else {
echo “Upload failed”;
echo “</p>”;
echo ‘<pre>’;
echo ‘Here is some more debugging info:’;
print_r($_FILES);
print “</pre>”;

}

?>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

-=-=-=-=-=

libcurl

allows  to connect and communicate to many different types of servers with many different types of protocols.

libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols.

libcurl also supports HTTPS certificates,

HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.

 

CURLOPT_RETURNTRANSFER TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.

http://php.net/manual/en/intro.curl.php

——————-

To  upload file to the webservice from php is the same as sending it from a form,

for uploading from php to a webservice

$fullflepath = 'C:\temp\test.jpg';
$upload_url = 'http://www.example.com/uploadtarget.php';
$params = array
(
 'photo'=>"@$fullfilepath",
 'title'=>$title
);  

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
curl_close($ch);

-=-=-=-=-=-=-=-=-=-=-=-=-

Superglobals Attributes

“Superglobals” are defined PHP variables which can be used/accessed in any scope or from any file, class or function with ease.

These are the main PHP superglobal variables:

  • $_SESSION
  • $_SERVER
  • $_POST
  • $_COOKIE
  • $GLOBALS
  • $_FILES
  • $_REQUEST
  • $_ENV
  • $_GET

to see what you have got:

var_dump($_POST);

var_dump($_FILES);

https://www.bitdegree.org/learn/php-global-variable/

The webservice accepting a file

use the superglobal variable:$_FILES (doc) to get upload files.

$_FILES is a ‘superglobal’, or automatic global, variable. It is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods. 

An associative array of items uploaded to the current script via the HTTP POST method.

The global $_FILES will contain all the uploaded file information. Its contents from the example form is as follows. Note that this assumes the use of the file upload name userfile, as used in the example script above. This can be any name.

$_FILES[‘userfile’][‘name’]

The original name of the file on the client machine.

$_FILES[‘userfile’][‘type’]

The mime type of the file, if the browser provided this information. An example would be “image/gif”. This mime type is however not checked on the PHP side and therefore don’t take its value for granted.

$_FILES[‘userfile’][‘size’]

The size, in bytes, of the uploaded file.

$_FILES[‘userfile’][‘tmp_name’]

The temporary filename of the file in which the uploaded file was stored on the server.

$_FILES[‘userfile’][‘error’]

The error code associated with this file upload.

Files will, by default be stored in the server’s default temporary directory, unless another location has been given with the upload_tmp_dir directive in php.ini. The server’s default directory can be changed by setting the environment variable TMPDIR in the environment in which PHP runs. Setting it using putenv() from within a PHP script will not work. This environment variable can also be used to make sure that other operations are working on uploaded files, as well.

$uploads_dir = '/uploads';
foreach ($_FILES["photo"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["photo"]["tmp_name"][$key];
        $name = $_FILES["photo"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}

http://php.net/manual/en/features.file-upload.post-method.php

http://php.net/manual/en/features.file-upload.php

=-=-=-=-=-=-=-

Add a line to a file on the server.


<?php
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file, 
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
?>

=-=-=-=-=-=-==-=-=-

PHP web services

http://php.net/manual/en/refs.webservice.php

-=-=-=-=-

PHP courses

https://www.w3schools.com/php/default.asp

 

Loading