cancel
Showing results for 
Search instead for 
Did you mean: 

php - moving file from webserver to application server

FIXED
chenks76
All Star
Posts: 3,274
Thanks: 338
Fixes: 12
Registered: ‎24-10-2013

php - moving file from webserver to application server

todays conundrum.
I've come up with a PHP script that uploads a file from a web page to the webserver (and as my PHP is very rough to say the least it's probably quite brutal, but works).

the file gets uploaded to a folder on the webserver. all OK so far.
the problem is that the file needs to reside on a different server (an application server with no web services).
so i need the script to move (not copy) the file to the desired location on the application server once the initial upload had completed.

is this possible with PHP? the webserver can communicate with the application server via a domain account so comms between the two isn't a problem.

 

<?php
include 'database.php';
$conn = sqlsrv_connect($server, $connArr);

$target_server = "\\\CTECH-GLA-VWEB2";
$target_dir = "c:/speco_uploads/";
$category = $_POST["uploadcategory"];
$filedescription = $_POST["uploaddescription"];
$callRef = $_POST['ctsref'];
$filestamp = $_POST['filestamp'];
$loggedinuser = $_POST['ctsuser'];
$target_file = $target_dir . $callRef . "_" . $category . "_" . $filestamp . "_" . basename($_FILES["fileToUpload"]["name"]);
$uploaded_file = $target_server . "\\speco_uploads\\" . $callRef . "_" . $category . "_" . $filestamp . "_" . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, a file with this name already exists.<br />";
    $uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 6000000) {
    echo "Your file is too large.<br />";
    $uploadOk = 0;
}

// Allow certain file formats
if($FileType != "pdf" && $FileType != "PDF" && $FileType != "jpg" && $FileType != "JPG" && $FileType != "jpeg" && $FileType != "JPEG" ) {
    echo "Only JPG or PDF files are allowed.<br />";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Your file was NOT uploaded.";
    
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    	$query = "INSERT INTO linked_documents (LD_Link_Type, LD_Link_reference, LD_Category, LD_Description, LD_DateTime, LD_added_by, LD_Link_to_CD, LD_embedded, LD_Filename, LD_Image, LD_Document_Type, LD_Link_to_ID) VALUES ('J','$callRef','$category','$filedescription',getdate(),'$loggedinuser','CTS','0','$uploaded_file',NULL,'$FileType','0')";
	
		$stmt = sqlsrv_query( $conn, $query );
		if( $stmt === false ) {
	     die( print_r( sqlsrv_errors(), true));
		}

        echo "The file has been uploaded.<br /><br />";
        echo "File Name : ". basename( $_FILES["fileToUpload"]["name"]). "<br />";
        echo "Category : ". $category."<br />";
        echo "Call Ref: ". $callRef."<br />";
        echo "Uploaded By: ". $loggedinuser;
    } else {
        echo "There was an error uploading your file.";
    }
}
?>
6 REPLIES 6
decomplexity
Rising Star
Posts: 493
Thanks: 26
Registered: ‎30-07-2007

Re: php - moving file from webserver to application server

I suspect you will need to suck it and see.

I formerly had  many applications that moved files via PHP FTP from PN CGI servers to PN home pages servers, and this also worked when PN CGI was replaced by PAYH.  But after PN’s final move to Hostopia, outbound FTP (i.e. sessions initiated by PHP from the Hostopia side) were blocked by Hostopia’s firewall ‘for security’.

So if your PHP application runs on Hostopia’s servers but the database server is outside Hostopia altogether, everything depends on their firewall rules (i.e. ‘communication’ might work but outbound data transfer might be blocked)  

Zen from May 17. PN Business account from 2004 - 2017
chenks76
All Star
Posts: 3,274
Thanks: 338
Fixes: 12
Registered: ‎24-10-2013

Re: php - moving file from webserver to application server

i should have clarified, this is not plusnet hosted.
these are our own windows servers.
7up
Community Veteran
Posts: 15,828
Thanks: 1,583
Fixes: 17
Registered: ‎01-08-2007

Re: php - moving file from webserver to application server

Careful not to make that script location public checks, your sql with variables is an injection attack waiting to happen.

Switch to pdo and prepared statements of this is going to be exposed on a public server.
I need a new signature... i'm bored of the old one!
chenks76
All Star
Posts: 3,274
Thanks: 338
Fixes: 12
Registered: ‎24-10-2013

Re: php - moving file from webserver to application server

it's not public facing
7up
Community Veteran
Posts: 15,828
Thanks: 1,583
Fixes: 17
Registered: ‎01-08-2007

Re: php - moving file from webserver to application server

Ok that's good
I need a new signature... i'm bored of the old one!
chenks76
All Star
Posts: 3,274
Thanks: 338
Fixes: 12
Registered: ‎24-10-2013

Re: php - moving file from webserver to application server

Fix
actually scratch that.
i've just realised that it can directly upload to the application server due to the IIS anonymouse access link.
so uploading to the correct location is working.