Read excel file from another server

Hi,

I am trying to read the excel file from another server but i don’t know how to do it.
I tried to save the excel file and read from my server and it works perfectly, it just i want to read directly from another server. What should i do?

Here is my code reading from my server.

require_once 'Classes/PHPExcel.php';

//setup connection to MSSQL DB
$serverName = "MyServer"; //serverName\instanceName=
$connectionInfo = array( "Database"=>"myTable", "UID"=>"user", "PWD"=>"abc123");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
    echo "Connection established.<br />";
}else{
    echo "Connection could not be established.<br />";
    die( print_r( sqlsrv_errors(), true));
}



$inputFileBuild = 'D:\Inetpub\wwroot\test\excel.xls';
$inputFileType = PHPExcel_IOFactory::identify($inputFileBuild);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileBuild);

$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestDataRow();



$x1 = 0;
$x2 = 0;

$sql_delNULL = "TRUNCATE TABLE detail";
$stmt_delNULL = sqlsrv_query( $conn, $sql_delNULL);
if( $stmt_delNULL === false ) {
    die( print_r( sqlsrv_errors(), true));
}else{
    echo "Table: Data Deleted. \n";
}

 $sql_delDetailTable = "TRUNCATE TABLE detail";
$stmt_delDetailTable = sqlsrv_query( $conn, $sql_delDetailTable);
if( $stmt_delDetailTable === false ) {
    die( print_r( sqlsrv_errors(), true));
}else{
    echo "Detail_Table : Data Deleted. \n";
}
 
for($row = 1; $row <= $highestRow; ++$row) {
	//switch($row){
		/* case 1:
			//skip first row
			break;
		default: */
		if($row != 1)
		{
			$ordernum= $sheet->getCell('A'.$row)->getValue();
		
			$order_date = $sheet->getCell('C'.$row)->getValue(); /* != null ? date("m/d/Y", PHPExcel_Shared_Date::ExcelToPHP($sheet->getCell('C'.$row)->getValue())) : NULL; */
			
			$location = $sheet->getCell('H'.$row)->getValue();
			$status_code = $sheet->getCell('K'.$row)->getValue();
			
			$orderDate =  date('m-d-Y', strtotime($order_date));
			

				$sql = "INSERT INTO detail(order_num,location, status_code,order_date) VALUES (?,?,?,?,)";
				$params = array($order_number,  $location, $status_code,$orderDate);
				
				
            $stmt = sqlsrv_query( $conn, $sql, $params);
            if( $stmt === false ) {
                echo '<pre>';die( print_r( sqlsrv_errors(), true));echo '</pre>';
            }
		}
		else
		{
			$row++;
		}			

	//}
}
 echo "Main Table Updated : DONE"."\n"; 








// Close the connection. */
sqlsrv_close($conn);

Thank you in advance :slight_smile:

by reading it from another server, do you have access to that server of are you scraping it?

Yes, i already have an access into it

If the file is in a publicly accessible location, file_get_contents, or using cURL would both be ways to do this.

Oh. Do you have any sample on file_get_content?

$file_location = "url";
$contents = file_get_contents($file_location);
if(empty($contents)) {
    log("Could not read file at, {$file_location}"); 
}

Hi,
can the url be like this ‘MyServer\MyFolder\Excel.xls’ ?

That’s a uri. Is the file on a different server or the same server?

Different server. Can the uri be the path in reading excel file?

You need the full location, that means the url along with the path, and the file name

Sponsor our Newsletter | Privacy Policy | Terms of Service