Get file name after remote file grabbing - php

I'm using the PHP file grabber script. I put URL of the remote file on the field and then the file is directly uploaded to my server. The code looks like this:
<?php
ini_set("memory_limit","2000M");
ini_set('max_execution_time',"2500");
foreach ($_POST['store'] as $value){
if ($value!=""){
echo("Attempting: ".$value."<br />");
system("cd files && wget ".$value);
echo("<b>Success: ".$value."</b><br />");
}
}
echo("Finished all file uploading.");
?>
After uploading a file I would like to display direct url to the file : for example
Finished all file uploading, direct URL:
http://site.com/files/grabbedfile.zip
Could you help me how to determine file name of last uploaded file within this code?
Thanks in advance

You can use wget log files. Just add -o logfilename.
Here is a small function get_filename( $wget_logfile )
ini_set("memory_limit","2000M");
ini_set('max_execution_time',"2500");
function get_filename( $wget_logfile )
{
$log = explode("\n", file_get_contents( $wget_logfile ));
foreach ( $log as $line )
{
preg_match ("/^.*Saving to: .{1}(.*).{1}/", $line, $find);
if ( count($find) )
return $find[1];
}
return "";
}
$tmplog = tempnam("/tmp", "wgetlog");
$filename = "";
foreach ($_POST['store'] as $value){
if ($value!=""){
echo("Attempting: ".$value."<br />");
system("cd files && wget -o $tmplog ".$value); // -o logfile
$filename = get_filename( $tmplog ); // current filename
unlink ( $tmplog ); // remove logfile
echo("<b>Success: ".$value."</b><br />");
}
}
echo("Finished all file uploading.");
echo "Last file: ".$filename;

Instead of using wget like that, you could do it all using cURL, if that is available.
<?php
set_time_limit(0);
$lastDownloadFile = null;
foreach ($_POST['store'] as $value) {
if ($value !== '' && downloadFile($value)) {
$lastDownloadFile = $value;
}
}
if ($lastDownloadFile !== null) {
// Print out info
$onlyfilename = pathinfo($lastDownloadFile, PATHINFO_BASENAME);
} else {
// No files was successfully uploaded
}
function downloadFile($filetodownload) {
$fp = fopen(pathinfo($filetodownload, PATHINFO_BASENAME), 'w+');
$ch = curl_init($filetodownload);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // We're writing to our file pointer we created earlier
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Just in case the server throws us around
$success = curl_exec($ch); // gogo!
// clean up
curl_close($ch);
fclose($fp);
return $success;
}
Some words of caution however, letting people upload whatever to your server might not be the best idea. What are you trying to accomplish with this?

Related

Unable to fread output of a remote php file

I am using the output of a php file on a remote server, to show content on my own web-site. I do not have access to modify files on the remote server.
The remote php file outputs java script like this:
document.write('<p>some text</p>');
If I enter the url in a browser I get the correct output. E.g:
https://www.remote_server.com/files/the.php?param1=12
I can show the output of the remote file on my website like this:
<script type="text/javascript" src="https://www.remote_server.com/files/the.php?param1=12"></script>
But I would like to filter the output a bit before showing it.
Therefore I implemented a php file with this code:
function getRemoteOutput(){
$file = fopen("https://www.remote_server.com/files/the.php?param1=12","r");
$output = fread($file,1024);
fclose($file);
return $output;
}
When I call this function fopen() returns a valid handle, but fread() returns an empty string.
I have tried using file_get_contents() instead, but get the same result.
Is what I am trying to do possible?
Is it possible for the remote server to allow me to read the file via the browser, but block access from a php file?
Your variable $output is only holding the 1st 1024 bytes of the url... (headers maybe?).
You will need to add a while not the "end of file" loop to concatenate the entire remote file.
PHP reference: feof
You can learn a lot more in the PHP description for the fread function.
PHP reference: fread.
<?php
echo getRemoteOutput();
function getRemoteOutput(){
$file = fopen("http://php.net/manual/en/function.fread.php","r");
$output = "";
while (!feof($file)){ // while not the End Of File
$output.= fread($file,1024); //reads 1024 bytes at a time and appends to the variable as a string.
}
return $output;
fclose($file);
}
?>
In regards to your questions:
Is what I am trying to do possible?
Yes this is possible.
Is it possible for the remote server to allow me to read the file via
the browser, but block access from a php file?
I doubt it.
I contacted the support team for the site I was trying to connect to. They told me that they do prevent access from php files.
So that seems to be the reason for my problems, and apparently I just cannot do what I tried to do.
For what it's worth, here is the code I used to test the various methods to read file output:
<?php
//$remotefile = 'http://www.xencomsoftware.net/configurator/tracker/ip.php';
$remotefile = "http://php.net/manual/en/function.fread.php";
function getList1(){
global $remotefile;
$output = file_get_contents($remotefile);
return htmlentities($output);
}
function getList2(){
global $remotefile;
$file = fopen($remotefile,"r");
$output = "";
while (!feof($file)){ // while not the End Of File
$output.= fread($file,1024); //reads 1024 bytes at a time and appends to the variable as a string.
}
fclose($file);
return htmlentities($output);
}
function getList3(){
global $remotefile;
$ch = curl_init(); // create curl resource
curl_setopt($ch, CURLOPT_URL, $remotefile); // set url
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //return the transfer as a string
$output = curl_exec($ch); // $output contains the output string
curl_close($ch); // close curl resource to free up system resources
return htmlentities($output);
}
function getList4(){
global $remotefile;
$r = new HttpRequest($remotefile, HttpRequest::METH_GET);
try {
$r->send();
if ($r->getResponseCode() == 200) {
$output = $r->getResponseBody();
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
return htmlentities($output);
}
function dumpList($ix, $list){
$len = strlen($list);
echo "<p><b>--- getList$ix() ---</b></p>";
echo "<div>Length: $len</div>";
for ($i = 0 ; $i < 10 ; $i++) {
echo "$i: $list[$i] <br>";
}
// echo "<p>$list</p>";
}
dumpList(1, getList1()); // doesn't work! You cannot include/requre a remote file.
dumpList(2, getList2());
dumpList(3, getList3());
dumpList(4, getList4());
?>

How to handle multiple request to write a file in php?

I have this code
<?php
if (!file_exists($folderAddress . $_GET['name'] . '.json')) {
//create file
$myJson = fopen($folder.$_GET['name']. ".json", "w");
//get a context of a url
$ch = curl_init($myUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$text = '';
if( ($text = curl_exec($ch) ) === false)
{
die('fail');
}
// Close handle
curl_close($ch);
//copy to json file
$result = fwrite($myJson, $text);
fclose($myJson);
$t = time();
//add update date to db
if (!mysqli_query($con, "INSERT INTO test (name )
VALUES ('$_GET['name]', '$t')")
) {
echo $text;
mysqli_close($con);
die('');
}
//show the json file
echo $text;
}
?>
And i have this problem if users request this file in same time or with less than a 500 ms delay all of them think that the file does not exist. So how can i prevent users to write file when the first is writing on it ?
You use exclusive lock when writing to a file, so the other processes can't interfere until initial one is complete. You also don't need fopen / fwrite whatsoever.
Fetch your data using cURL and use the following snippet:
file_put_contents($filename, $contents, LOCK_EX);

Download Campaign Performance Reports using Bings Ads in PHP

I was struck in this since a weeks. Please tell me if any one can help me out from this.
I tried this samples they given. I'm trying to download only campaign performance reports, Where i'm able to download a zip file which has a csv file in it. Here is the another direct example i followed for keywords and did the same way for campaign performance. Which giving me a link to download the reports. When i'm trying to download the url manually i can download but I cannot download it through my code.
function DownloadFile($reportDownloadUrl, $downloadPath) {
if (!$reader = fopen($reportDownloadUrl, 'rb')) {
throw new Exception("Failed to open URL " . $reportDownloadUrl . ".");
}
if (!$writer = fopen($downloadPath, 'wb')){
fclose($reader);
throw new Exception("Failed to create ZIP file " . $downloadPath . ".");
}
$bufferSize = 100 * 1024;
while (!feof($reader)) {
if (false === ($buffer = fread($reader, $bufferSize))) {
fclose($reader);
fclose($writer);
throw new Exception("Read operation from URL failed.");
}
if (fwrite($writer, $buffer) === false) {
fclose($reader);
fclose($writer);
$exception = new Exception("Write operation to ZIP file failed.");
}
}
fclose($reader);
fflush($writer);
fclose($writer);
}
But I couldn't download the file. I can't move forward from there so any help like downloading reports in any-other form or the simple code changes in current method is greatly appreciated. Thanks in advance.
When I tried this I also had problems with the DownloadFile function so replaced this with a different version
function DownloadFile($reportDownloadUrl, $downloadPath) {
$url = $reportDownloadUrl;
// Example for the path would be in this format
// $path = '/xxx/yyy/reports/keywordperf.zip';
// using the server path and not relative to the file
$path = $downloadPath;
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_FILE, $fp);
if($result = curl_exec($ch)) {
$status = true;
}
else
{
$status = false;
}
curl_close($ch);
fclose($fp);
return status;
}

PHP Simultaneous file access / flock() issue

I'm having trouble implementing a script which should parse a json string from a file, either overwrite the object with the same id or add it to the json array and write it back to the file. The script is called in a for loop so that I tried to use flock to prevent overwriting the file before the json string could be parsed, but the results look really strange and I don't know whats going wrong. Here is the script:
$method = $_SERVER['REQUEST_METHOD'];
$file = fopen($userid . '.json', 'c+');
flock($file, LOCK_EX);
$jsonStr = (filesize($userid . '.json') == 0 ) ? '{"success": true}' : fread($file, filesize($userid . '.json'));
$jsonObj = json_decode($jsonStr);
if($method === 'POST') {
$dataStr = file_get_contents('php://input');
$dataObj = json_decode($dataStr);
$replacements = array();
if(isset($jsonObj->images)) {
foreach($jsonObj->images as $idx=>$image) {
if($image->id == $dataObj->id) {
$replacements += array($idx => $dataObj);
}
}
if(count($replacements)==0) {
array_push($replacements, $dataObj);
}
$jsonObj->images = array_replace($jsonObj->images, $replacements);
} else {
//$jsonObj = new stdClass();
$jsonObj->images = new stdClass();
$jsonObj->images = array($dataObj);
}
fwrite($file, json_encode($jsonObj));
file_put_contents('log.json', json_encode($dataObj), FILE_APPEND);
echo '{"success":true, "images":' . $dataStr . '}';
} else if($method === 'GET') {
echo $jsonStr;
}
fclose($file);
As result I get sometime three files one just called ".json" and the other one correctly userid.json and the log.json. The log looks best, since all 26 objects are shown correctly, but without checking for exiting objects previously. The ".json" is just blank and the looks really odd:
{"success":true,"images":[{"id":"f9f4b6ee-8b7b-414e-98f4-47ed5ee3c594","top":1200,"left":4050,"clicks":0,"affinity":[]}]}
{"success":true,"images":[{"id":"034c7661-9466-4651-b860-7e049e1543ac","top":900,"left":600,"clicks":0,"affinity":[]}]}
{"images":[{"id":"eebc35ec-6c0e-416a-9516-061df821083d","top":1800,"left":3300,"clicks":0,"affinity":[]}]}
{"images":[{"id":"e09da5b9-2e65-4a12-94cd-4745b354a256","top":1200,"left":1200,"clicks":0,"affinity":[]}]}
{"images":[{"id":"b023a259-4554-48a5-acd8-cb4215e6a391","top":1350,"left":1500,"clicks":0,"affinity":[]}]}
{"images":[{"id":"6521d8c3-461c-4fcd-b69d-69f14ae37418","top":600,"left":3750,"clicks":0,"affinity":[]}]}
{"images":[{"id":"4c082c1a-d4ae-4c1c-bfc4-ed36980f5db2","top":3150,"left":600,"clicks":0,"affinity":[]}]}
{"images":[{"id":"22dbdea2-4936-4353-825e-9af6e6f2ca93","top":3000,"left":2100,"clicks":0,"affinity":[]}]}
{"images":[{"id":"a7ba32d4-912e-489d-8f9b-e412bc1629c1","top":2850,"left":3750,"clicks":0,"affinity":[]}]}
{"images":[{"id":"b258148d-1779-4f17-ae5e-1160aa0a34b9","top":750,"left":4050,"clicks":0,"affinity":[]}]}
{"images":[{"id":"21c1c9d1-a7a6-48d2-a4ba-174c0fe248a3","top":1650,"left":750,"clicks":0,"affinity":[]}]}
{"images":[{"id":"bfed2f56-7876-4eb2-a217-e0f71f4455ee","top":3300,"left":1500,"clicks":0,"affinity":[]}]}
{"images":[{"id":"7f13b674-5d07-444f-bf6d-463758c96788","top":1650,"left":1950,"clicks":0,"affinity":[]}]}
{"images":[{"id":"fb8711f2-ba10-44d7-9038-02cae6438506","top":1800,"left":450,"clicks":0,"affinity":[]}]}
{"images":[{"id":"ac83e869-e4fb-4eee-8d2e-4d26762101d9","top":750,"left":1800,"clicks":0,"affinity":[]}]}
{"images":[{"id":"069a7595-2271-4430-b324-974115df7b80","top":2550,"left":1800,"clicks":0,"affinity":[]}]}
{"images":[{"id":"ea9ab2fc-e179-4f56-9ce7-8af456911b33","top":1950,"left":750,"clicks":0,"affinity":[]}]}
{"images":[{"id":"9b997ae3-0c66-4f78-937a-2fde2470d7d7","top":300,"left":300,"clicks":0,"affinity":[]}]}
{"images":[{"id":"f860dea4-3b27-4401-999b-72ce45022110","top":1200,"left":1950,"clicks":0,"affinity":[]}]}
{"images":[{"id":"2633c107-8a51-4e9c-bc2c-f87c30f7359d","top":750,"left":2100,"clicks":0,"affinity":[]}]}
{"images":[{"id":"bebcb2bb-c2f0-4e4d-b202-b05090eacf49","top":3600,"left":3600,"clicks":0,"affinity":[]}]}
{"images":[{"id":"460f961e-f1dd-4329-95be-d30770a37b83","top":2400,"left":1200,"clicks":0,"affinity":[]}]}
{"images":[{"id":"ee9b254f-fe6a-4ece-a662-e934625e992a","top":3600,"left":600,"clicks":0,"affinity":[]}]}
{"images":[{"id":"4e047ea9-345e-4cf9-a066-d0b431820c61","top":2100,"left":1350,"clicks":0,"affinity":[]}]}
{"images":[{"id":"8bbb5327-6d39-40e1-919e-d7b70e13fc2b","top":2700,"left":1200,"clicks":0,"affinity":[]}]}
{"images":[{"id":"0c50c0ec-e18b-4917-9787-bad245eed798","top":1200,"left":2550,"clicks":0,"affinity":[]}]}
I also get an exception:
<b>Strict Standards</b>: Creating default object from empty value in <b>C:\xampp\htdocs\gallery\store.php</b> on line <b>38</b><br />
I guess I didn't understand the procedure correctly and I'm happy for any help.
EDIT: The result I want, looks like this:
{"success":true,"images":[{"id":"f9f4b6ee-8b7b-414e-98f4-47ed5ee3c594","top":1200,"left":4050,"clicks":0,"affinity":[]},
{"id":"034c7661-9466-4651-b860-7e049e1543ac","top":900,"left":600,"clicks":0,"affinity":[]},{"id":"eebc35ec-6c0e-416a-9516-061df821083d","top":1800,"left":3300,"clicks":0,"affinity":[]},
{"id":"e09da5b9-2e65-4a12-94cd-4745b354a256","top":1200,"left":1200,"clicks":0,"affinity":[]},
{"id":"b023a259-4554-48a5-acd8-cb4215e6a391","top":1350,"left":1500,"clicks":0,"affinity":[]},
{"id":"6521d8c3-461c-4fcd-b69d-69f14ae37418","top":600,"left":3750,"clicks":0,"affinity":[]},
{"id":"4c082c1a-d4ae-4c1c-bfc4-ed36980f5db2","top":3150,"left":600,"clicks":0,"affinity":[]},
{"id":"22dbdea2-4936-4353-825e-9af6e6f2ca93","top":3000,"left":2100,"clicks":0,"affinity":[]},
{"id":"a7ba32d4-912e-489d-8f9b-e412bc1629c1","top":2850,"left":3750,"clicks":0,"affinity":[]},
{"id":"b258148d-1779-4f17-ae5e-1160aa0a34b9","top":750,"left":4050,"clicks":0,"affinity":[]},
{"id":"21c1c9d1-a7a6-48d2-a4ba-174c0fe248a3","top":1650,"left":750,"clicks":0,"affinity":[]},
{"id":"bfed2f56-7876-4eb2-a217-e0f71f4455ee","top":3300,"left":1500,"clicks":0,"affinity":[]},
{"id":"7f13b674-5d07-444f-bf6d-463758c96788","top":1650,"left":1950,"clicks":0,"affinity":[]},
{"id":"fb8711f2-ba10-44d7-9038-02cae6438506","top":1800,"left":450,"clicks":0,"affinity":[]},
{"id":"ac83e869-e4fb-4eee-8d2e-4d26762101d9","top":750,"left":1800,"clicks":0,"affinity":[]},
{"id":"069a7595-2271-4430-b324-974115df7b80","top":2550,"left":1800,"clicks":0,"affinity":[]},
{"id":"ea9ab2fc-e179-4f56-9ce7-8af456911b33","top":1950,"left":750,"clicks":0,"affinity":[]},
{"id":"9b997ae3-0c66-4f78-937a-2fde2470d7d7","top":300,"left":300,"clicks":0,"affinity":[]},
{"id":"f860dea4-3b27-4401-999b-72ce45022110","top":1200,"left":1950,"clicks":0,"affinity":[]},
{"id":"2633c107-8a51-4e9c-bc2c-f87c30f7359d","top":750,"left":2100,"clicks":0,"affinity":[]},
{"id":"bebcb2bb-c2f0-4e4d-b202-b05090eacf49","top":3600,"left":3600,"clicks":0,"affinity":[]},
{"id":"460f961e-f1dd-4329-95be-d30770a37b83","top":2400,"left":1200,"clicks":0,"affinity":[]},
{"id":"ee9b254f-fe6a-4ece-a662-e934625e992a","top":3600,"left":600,"clicks":0,"affinity":[]},
{"id":"4e047ea9-345e-4cf9-a066-d0b431820c61","top":2100,"left":1350,"clicks":0,"affinity":[]},
{"id":"8bbb5327-6d39-40e1-919e-d7b70e13fc2b","top":2700,"left":1200,"clicks":0,"affinity":[]},
{"id":"0c50c0ec-e18b-4917-9787-bad245eed798","top":1200,"left":2550,"clicks":0,"affinity":[]}]}
Cheers,
Daniel
I finally have it working and for anyone else having issues with simultaneous file access, here's my solution:
Check if the file exists, if not, create it and set up the data structure (to prevent the strict standard error)
$filename = $userid . '.json';
if(!file_exists($filename)) {
$file = fopen($filename, 'w');
if(flock($file, LOCK_EX)) {
fwrite($file, '{"success": true, "images": []}');
flock($file, LOCK_UN);
fclose($file);
}
}
Open the file using c+ as second parameter (w+ would set the filesize to 0). Check if you can get the exclusive lock. Read the file and rewind in order to overwrite it later.
else {
$file = fopen($userid . '.json', 'c+');
if(flock($file, LOCK_EX)) {
$jsonStr = fread($file, filesize($userid . '.json'));
$jsonObj = json_decode($jsonStr);
rewind($file);
Manipulate the object how you need it and write it back as encoded json string using fwrite
if($method === 'POST') {
$dataStr = file_get_contents('php://input');
if(isset($dataStr) &&$dataStr != '') {
$dataObj = json_decode($dataStr);
$replace = -1;
foreach($jsonObj->images as $idx=>$image) {
if($image->id == $dataObj->id) {
$replace = $idx;
}
}
if($replace != -1) {
$img = $jsonObj->images;
$img[$replace] = $dataObj;
$jsonObj->images = $img;
} else {
array_push($jsonObj->images, $dataObj);
}
fwrite($file, json_encode($jsonObj));
}
echo '{"success":true, "images":' . $dataStr . '}';
} else if($method === 'GET') {
echo $jsonStr;
}
Release the lock and close the file.
flock($file, LOCK_UN);
fclose($file);
}
}

How to check if a list of domain names have a site?

I have a huge list of domain names in the form of abcde.com
What I have to do is to check if the domains have a page otherwise I get the server not found message.
What is a code that will check this automatically and return me something if there is a site ? I am familiar with PHP.
Thank you.
Something simple would be:
foreach ($domains as $domain) {
$html = file_get_contents('http://'.$domain);
if ($html) {
//do something with data
} else {
// page not found
}
}
If you have them in a txt file, with each line containing the domain name you could do this:
$file_handle = fopen("mydomains.txt", "r");
while (!feof($file_handle)) {
$domain = fgets($file_handle);
//use code above here
}
}
fclose($file_handle);
You can connect to each domain/hostname using cURL.
Example:
// I'm assuming one domain per line
$h = fopen("domains.txt", "r");
while (($host = preg_replace("/[\n\r]/", "", fgets($h))) !== false) {
$ch = curl_init($host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (curl_exec($ch) !== false) {
// code for domain/host with website
} else {
// code for domain/host without website
}
curl_close($ch);
}

Categories