I want to download file .xls and then upload it to database oracle. but I am getting an error "the filename .xls is not readable".
Below is my script:
<? require_once (dirname(__FILE__)."/upload/upBc.php");
if(!empty($_FILES["file"])){
echo $file = $_FILES["file"];
}else{
/*$file = "/opt/lampp/htdocs/avar/pp/ccr/test.xls";*/
$file = '/www.upload.com/pm_ms/test.xls';
/* $file = "website_data_". date('ymd').".xls";*/
}
$upload = new upload();
$upload->setFile($file);
$upload->getFileType();
$upload->getFileName();
$upload->getFileDir();
$upload->readDataTbc();
$dataD = array ();
$dataD = $upload->getDataTbc();
echo "<table border = 1>";
for($i=1;$i < count($dataD);$i++){
echo "<tr>";
for($j=0;$j < $upload->getHSize();$j++){
echo " <td>".$dataD[$i][$j]."</td>";
}
echo"</tr>";
}
echo"</table>";
echo"data length=". count($dataD);
$upload->commitDataTbc();
?>
If I use $file = "/opt/lampp/htdocs/avar/pp/ccr/test.xls";. in my program it works
but if I use $file = '/www.upload.com/pm_ms/test.xls';. I get the error THE FILENAME test.xls is Not Readable
That's probably because /www.upload.com/ doesn't exist on your server.
Do you know the actual path to the file? By prefixing the URI with / this makes it absolute, which means that it's trying to read literally from /www.upload.com on your server. If you're looking to make this relative you could try ./upload.com/ or simply upload.com/ as the path prefix.
If you are however trying to load it from the website upload.com (instead of a folder from your server) you have two options:
(If Supported) Set the filename as a URL ie. $file = 'http://www.upload.com/pm_ms/test.xls';
Download the file and then open it locally.
For #2:
$file = 'test.xls';
$ctnts = file_get_contents('http://www.upload.com/pm_ms/test.xls');
file_put_contents($file, $ctnts);
Related
I can download a csv file via a fixed url. The url just doesn't have a file name.
When I open the url in a browser, the file export.csv is generated and after about 10 sec the download starts.
Now I want to download this export csv file directly to my own server,
I have the code below, but it doesn't work in my case.
Can you help me? my thanks are big
<?php
$url =
'https://www.example.com/product-export/link123456789';
$file_name = basename($url);
if (file_put_contents($file_name, file_get_contents($url)))
{
echo "File downloaded successfully";
}
else
{
echo "File downloading failed.";
}
?>
As the generated output filename is always export.csv, we may use the following to specify the filename:
$file_name = dirname(__FILE__) . "/export.csv";
(make sure the directory is write-permitted so that the php can save the file "export.csv")
Hence, the code will be:
<?php
$url = 'http://www.createchhk.com/SOanswers/export1a.csv';
//$file_name = basename($url);
$file_name = dirname(__FILE__) . "/export.csv";
if (file_put_contents($file_name, file_get_contents($url))) {
echo "File downloaded successfully";
} else {
echo "File downloading failed.";
}
?>
For your cronjob, it will be something like :
1 1 * * * php /path_to_the_php/test.php
I am uploading files to a server using php and while the move_uploaded_file function returns no errors, the file is not in the destination folder. As you can see I am using the exact path from root, and the files being uploaded are lower than the max size.
$target = "/data/array1/users/ultimate/public_html/Uploads/2010/";
//Write the info to the bioHold xml file.
$xml = new DOMDocument();
$xml->load('bioHold.xml');
$xml->formatOutput = true;
$root = $xml->firstChild;
$player = $xml->createElement("player");
$image = $xml->createElement("image");
$image->setAttribute("loc", $target.basename($_FILES['image']['name']));
$player->appendChild($image);
$name = $xml->createElement("name", $_POST['name']);
$player->appendChild($name);
$number = $xml->createElement("number", $_POST['number']);
$player->appendChild($number);
$ghettoYear = $xml->createElement("ghettoYear", $_POST['ghetto']);
$player->appendChild($ghettoYear);
$schoolYear = $xml->createElement("schoolYear", $_POST['school']);
$player->appendChild($schoolYear);
$bio = $xml->createElement("bio", $_POST['bio']);
$player->appendChild($bio);
$root->appendChild($player);
$xml->save("bioHold.xml");
//Save the image to the server.
$target = $target.basename($_FILES['image']['name']);
if(is_uploaded_file($_FILES['image']['tmp_name']))
echo 'It is a file <br />';
if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
echo $_FILES['image']['error']."<br />";
}
else {
echo $_FILES['image']['error']."<br />";
echo $target;
}
Any help is appreciated.
Eric R.
Most like this is a permissions issue. I'm going to assume you don't have any kind of direct shell access to check this stuff directly, so here's how to do it from within the script:
Check if the $target directory exists:
$target = '/data/etc....';
if (!is_dir($target)) {
die("Directory $target is not a directory");
}
Check if it's writeable:
if (!is_writable($target)) {
die("Directory $target is not writeable");
}
Check if the full target filename exists/is writable - maybe it exists but can't be overwritten:
$target = $target . basename($_FILES['image']['name']);
if (!is_writeable($target)) {
die("File $target isn't writeable");
}
Beyond that:
if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
echo $_FILES['image']['error']."<br />";
}
Echoing out the error parameter here is of no use, it refers purely to the upload process. If the file was uploaded correctly, but could not be moved, this will still only echo out a 0 (e.g. the UPLOAD_ERR_OK constant). The proper way of checking for errors goes something like this:
if ($_FILES['images']['error'] === UPLOAD_ERR_OK) {
// file was properly uploaded
if (!is_uploaded_File(...)) {
die("Something done goofed - not uploaded file");
}
if (!move_uploaded_file(...)) {
echo "Couldn't move file, possible diagnostic information:"
print_r(error_get_last());
die();
}
} else {
die("Upload failed with error {$_FILES['images']['error']}");
}
You need to make sure that whoever is hosting your pages has the settings configured to allow you to upload and move files. Most will disable these functions as it's a sercurity risk.
Just email them and ask whether they are enabled.
Hope this helps.
your calls to is_uploaded_file and move_uploaded_file vary. for is_uploaded_file you are checking the 'name' and for move_uploaded_file you are passing in 'tmp_name'. try changing your call to move_uploaded_file to use 'name'
I am trying to upload Excel file using PHP. I am using WAMP server and written PHP code to upload Excel file in the same folder.
I have saved an Excel file so if I upload the saved file then it works fine but if I try to upload another Excel file from different location i.e. from desktop it is showing that abc.xls is not readable.
My code is as follows:
<?php
ini_set("display_errors",1);
require_once 'excel_reader2.php';
require_once 'db.php';
if(isset($_POST["btnImport"]))
{
if(!empty($_FILES["excelFile"]["tmp_name"]))
{
$fileupload = $_FILES["excelFile"]["name"];
$fileName = explode(".",$_FILES["excelFile"]["name"]);
if($fileName[1]=="xls"||$fileName[1]=="xlsx")
{
$data = new Spreadsheet_Excel_Reader($fileupload);
//echo "Total Sheets in this xls file: ".count($data->sheets)."<br /><br />";
$html="<table border='1'>";
for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
{
if(count(#$data->sheets[$i]['cells'])>0) // checking sheet not empty
{
// echo "Sheet $i:<br /><br />Total rows in sheet $i ".count($data->sheets[$i]['cells'])."<br />";
for($j=1;$j<=count($data->sheets[$i]['cells']);$j++) // loop used to get each row of the sheet
{
$html.="<tr>";
for($k=1;$k<=count($data->sheets[$i]['cells'][$j]);$k++) // This loop is created to get data in a table format.
{
$html.="<td>";
#$html.=$data->sheets[$i]['cells'][$j][$k];
$html.="</td>";
}
#$data->sheets[$i]['cells'][$j][1];
#$ques = mysql_real_escape_string($data->sheets[$i]['cells'][$j][1]);
$opt1 = mysql_real_escape_string($data->sheets[$i]['cells'][$j][2]);
$opt2 = mysql_real_escape_string($data->sheets[$i]['cells'][$j][3]);
$opt3 = mysql_real_escape_string($data->sheets[$i]['cells'][$j][4]);
#$opt4 = mysql_real_escape_string($data->sheets[$i]['cells'][$j][5]);
#$correct = mysql_real_escape_string($data->sheets[$i]['cells'][$j][6]);
#$Level = mysql_real_escape_string($data->sheets[$i]['cells'][$j][7]);
#$Category = mysql_real_escape_string($data->sheets[$i]['cells'][$j][8]);
#$explain = mysql_real_escape_string($data->sheets[$i]['cells'][$j][9]);
$nithi = "INSERT INTO `question` VALUES (NULL,'".$ques."','".$opt1."','".$opt2."','".$opt3."','".$opt4."','".$correct."','".$Level."','".$Category."','".$explain."')";
if (!mysql_query($nithi,$connection))
{
die('Error: ' . mysql_error());
}
$result = mysql_query("SET NAMES utf8");//the main trick
$cmd = "select * from TAMIL";
$result = mysql_query($cmd);
}
}
}}}}
if(#$result){
echo "Questions uploaded successfully";
}
?>
How can I upload Excel file from different location?
When a file is uploaded in PHP, it is put into temp folders.
In your code, there is no call for moving the uploaded file from temporary location into the desired one. Instead you are only checking $_FILES["excelFile"]["tmp_name"] and then directly using $_FILES["excelFile"]["name"] which is obviously incorrect.
Use move_uploaded_file() function to move the file, see the docs: http://php.net/move_uploaded_file
I have a folder directory set up like this in my htdocs:
claas2/TractorPics\5484474\Received\ => and then a bunch of images inside
im using php to put the picture from MySQL database where the file path is stored rather than all the pictures and so that they can be changed easily.
php:
if ( $_REQUEST['rec_pic'] ) {
$order_id = $_POST['rec_pic'];
$sql = "SELECT * FROM `orders` WHERE `active` = '1' AND `order_id` = '$order_id'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$dir = $row['rec_pic'];
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
echo "<div>";
echo "<a href='#'><img src='".$file."' /></a>";
echo "</div>";
}
closedir($handle);
}
}
}
When i echo this out i get an error saying that that file directory does not exist but if i were to do src it in an image it works just fine. What am i doing wrong here?
the php file that get getting the request is in the directory
claas2\db\ajax
Pretty messy path you have:
claas2/TractorPics\5484474\Recieved\
Try:
claas2\TractorPics\5484474\Recieved\
or
claas2/TractorPics/5484474/Recieved/
but do not mix separators.
EDIT
You are mixing path with URL. And it works because you are using \ as path segment separator, which your browser converts to / while sending request to webserver and your server is now Windows based I guess, therefore \ is invalid as path separator for file system on server. Do
$path = str_replace('\\', `/`, $pathFromDb);
and use result instead of data from db as you use now.
I have this code to read a file for preview, but the downside is I have to download the file first from cloud and read from it, but it's a waste of space so I want to delete it after viewing a certain file. Is there an automatic way of doing this? Or do I have to integrate it to a close button?
// Get the container we want to use
$container = $conn->get_container('mailtemplate');
//$filename = 'template1.zip';
// upload file to Rackspace
$object = $container->get_object($filename);
//var_dump($object);
//echo '<pre>' . print_r($object,true) . '</pre>';
$localfile = $dir.$filename;
//echo $localfile;
$object->save_to_filename($localfile);
if($_GET['preview'] == "true")
{
$dir = "../mailtemplates/";
$file1 = $_GET['tfilename'];
$file = $dir.$file1;
$file2 = "index.html";
$info = pathinfo($file);
$file_name = basename($file,'.'.$info['extension']);
$path = $file_name.'/'.$file2;
$zip = new ZipArchive();
$zip->open($file);
$fp = $zip->getStream($path);
if(!$fp)
{
exit("faileds\n");
$zip->close();
unlink($dir.$filename);
}
else
{
$stuff = stream_get_contents($fp);
echo $stuff;
$zip->close();
if($stuff != null)
{
unlink($dir.$filename);
}
}
}
else
{
unlink($dir.$filename);
}
You didn't google this did ya?
Try Unlink
Edit:
Taking a look at this code, $zip->open($file); <-- is where you open the file. The file variable is set by:
"../mailtemplates/" . basename($_GET['tfilename'], '.' . $info['extension']) . '/' . "index.html"
So you're grabbing a relative directory and grabbing a filename as a folder, and going to that folder /index.html. Here's an example:
if you're in c:\ testing and you go to ../mailtemplates/ you'll be in c:\mailtemplates and then you're looking at file test.php but you're removing the file extension, so you'll be opening the location c:\mailtemplates\test\index.html so you open up that html file and read it. Then, you're trying to delete c:\mailtemplates\test.php
can you explain how any of that makes sense to you? 'cause that seems very odd to me.