How to list files inside public folder? - php

I have a persistent volume defined in server under www/public/images/logo/
I want to list all the files inside that folder. So far I have done following lines with no luck.
public function getFiles($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = 'https://myurl.de/images/logo/';
$data = $this->getFiles($url);
Can anybody please help ?

If you are want to list all files on same server then you can use php function scandir();
If you are want to list all files on remote server then you need to connect it using ftp like below code.
$connection = ftp_connect("HOST_NAME");
$login = ftp_login($connection, "FTP_USERNAME","FTP_PASSWORD");
ftp_pasv($connection, true);
$fileList = ftp_nlist($connection,'DIRECTORY_ON_FTP');
ftp_close($connection);

Related

File uploads using CURLFile fails in PHP7.4

I query an API to which I upload files but no files are uploaded when I post using PHP7.4, however, it works fine with PHP7.3.
Here's a snippet of my code:
public function upload($opts = array())
{
$files = array();
foreach($opts['files'] as $i => $file)
{
$files['files[' . $i . ']'] = new CURLFile($file);
}
unset($opts['files']);
$data = array_merge($files, array( "data" => json_encode($opts)));
$response = self::curlRequest( "https://api.example.com/", $data);
return $response;
}
public static function curlRequest($url, $data)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
curl_setopt($curl, CURLOPT_TIMEOUT, 300);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 300);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
So the upload function accepts a multidimensional array of values including an array of files with index 'files'. It iterates through the files, creating CURLFile objects then posts these along with the rest of the data to the API.
Using PHP7.4, the global variables $_REQUEST and $_FILES on the API server are always empty. With PHP7.3 these variables are populated with the sent data as expected.
On https://www.php.net/manual/en/migration74.new-features.php it states:
CURLFile now supports stream wrappers in addition to plain file names, if the extension has been built against libcurl >= 7.56.0.
Libcurl version is 7.58.
A related bug report has been submitted here https://bugs.php.net/bug.php?id=79013 regarding the Content-Length header missing resulting in no file uploads but it seems the PHP team thinks the problem is with the server, not PHP.
Does anyone has any idea how to upload files using PHP7.4?
Version PHP 7.4.4 fixed this issue. Seems it was indeed a bug.

Firebase database delete specific data by php

How can I remove firebase specific data? I use the
php Kreait\Firebase library.
$fg = $database->getReference('raw_check_out')->orderByChild('reciptno')->equalTo($recipt)->getSnapshot();
$reb = $fg->getValue();
$fg->remove();
but this is not working.
Based on your code example:
$fg = $database
->getReference('raw_check_out')
->orderByChild('reciptno')
->equalTo($recipt)
->getSnapshot();
Here $fg does not hold the reference, but the snapshot.
If you want to remove the reference after you have retrieved the data you need, you need the reference itself:
$fg = $database->getReference('raw_check_out');
$query = $fg->orderByChild('reciptno')->equalTo($recipt);
$reb = $query->getSnapshot()->getValue();
$fg->remove();
This function is for unsubscribing users if you want to remove "user-id-8776" and your structure is like:
public function unsubscribe($uid)
{
$ref = $this->database->getReference()->getChild('users')->getChild($uid)->orderByChild($uid)->getReference();
$path = $ref->getUri()->getPath();
$ref->remove();
}
For any other data is more or less the same, you just have to find the reference of data you want to delete it and then you can remove it.
Check Image
You have another option to resolve this problem. This solution given below:
$url = "https://<projectID>.firebaseio.com/chatList/".<jsonFileName>."json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result = curl_exec($ch);
curl_close($ch);

Collecting file with PHP CURL after validating request downloads an empty file

I am doing a system where one of my sites goes to the other to get documents.
On the first site I am using Curl to make a request to get the file wanted:
I am using the solution from Download file from URL using CURL :
function collect_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://example.com");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
return($result);
}
function write_to_file($text,$new_filename){
$fp = fopen($new_filename, 'w');
fwrite($fp, $text);
fclose($fp);
}
$curlUrl = 'http://site2.com/file-depository/14R4NP8JkoIHwIyjnexSUmyJibdpHs5ZpFs3NLFCxcs54kNhHj';
$new_file_name = "testfile-new.png";
$temp_file_contents = collect_file($curlUrl);
write_to_file($temp_file_contents,$new_file_name);
I am testing downloading an image. If i use a direct URL into $curlUrl , for instance http://site2.com/file-depository/image.png it works perfect.
What I am doing is that the URL http://site2.com/file-depository/14R4NP8JkoIHwIyjnexSUmyJibdpHs5ZpFs3NLFCxcs54kNhHj is then parsed and checked against a database to match the document requested, once there is a document matched I need to provide this document to the Curl response.
I have tried many ways to read the file but everytime i am getting a file on the other end but it is only 1kb in size (45 expected) and when trying to open it i get an error unkown file type etc.
On the second site, once the URL is validated here is what I have:
$file = readfile('some-image.png');
echo $file;
I am guessing there is part of the information which belongs to the file missing but can't figure it out, any pointers appreciated!
I have replaced
function write_to_file($text,$new_filename){
$fp = fopen($new_filename, 'w');
fwrite($fp, $text);
fclose($fp);
}
by file_put_contents($new_file_name,trim($temp_file_contents));
Please note the trim(), the issue was that I was apparently collecting some empty space in front of the file content.

Running a for loop on an ASP page

I was trying to download the results of a batch and was coding a program for this.
On an aspx file, I was able to write the following PHP code since the URL included the parameters:
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
for ($i=1; $i<15000; $i++) {
$url = "http://example.com/result.aspx?ClassId=342&TermId=95&StudentId=".$i;
$returned_content = get_data($url);
if (stripos($returned_content,'Roll') !== false) {
echo "Student ID:" . $i;
echo $returned_content;
}
}
However, when a result is queried on a .ASP file, the URL simply says 'results.asp' without any additional parameters. Is there a way to use CURL requests to run a for loop and download this data in a similar manner?
Thanks for any help!

Getting a file from my local server and uploading it to a remote server using php

I know there are many posts about this issue however I cannot find one single answer on here that describes EXACTLY what I am trying to do and I have been searching for HOURS...so hopefully you all can help me and steer me in the right direction, the main thing I cannot figure out how to do is get the source file and pass that to my update function where I will ftp that file to the remote server...
Basically what I am trying to do is the following:
1) Get a file from my local host (ex. /home/user/public_html/file.php)
2) Upload that file to a remote server of one of my clients (I have all of their FTP information stored in my database and can connect to their server just fine)
3) If the file exists on the remote server, have it over write with the one.
Now, I have tried ftp_get to get the file but that is not what I want to do because I don't need to re-write or store the same file to the local server...
Here's what I have so far...
Any help is much appreciated and I do apologize if this is answered some where else I just didn't see it! :)
AS AN UPDATE TO THIS I JUST THOUGHT OF SOMETHING...
Why do I even have to get the local file via ftp? Shouldn't I just be able to get the local file some other way and pass that to my update function where it will upload that file to the remote server?
update.php
$updater = new updater($accountInfo['ftp_user'],$accountInfo['ftp_pass'],$accountInfo['host']);
$file = "relative/path/to/my/file";
$source = $updater->getFile($file);
$updater->update($source,$file);
my update class
class updater {
var $ftp_user = "";
var $ftp_pass = "";
var $ftp_host = "";
function __construct($user,$pass,$host) {
$this->ftp_user = $user;
$this->ftp_pass = $pass;
$this->ftp_host = $host;
}
function getFile($file) {
$curl = curl_init();
$file = fopen($file, 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://domain.com/".$file); #input
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "ftpuser:ftppass");
$returnFile = curl_exec($curl);
return $returnFile;
}
function update($source,$file) {
echo("Source: ".$source."<BR>");
echo("File: ".$file."<BR>");
$connection = ftp_connect($this->ftp_host);
$login = ftp_login($connection, $this->ftp_user, $this->ftp_pass);
if (!$connection || !$login) { die('Connection attempt failed!'); }
$upload = ftp_put($connection, "/home/".$this->ftp_host."/public_html/".$file, $source, FTP_BINARY);
if (!$upload) {
echo 'FTP upload failed!';
} else {
echo("UPDATED FILE ".$source." SUCCESSFULLY!<BR>");
}
ftp_close($connection);
}
Try this:
# Declare Files
$local_file = "/home/user/public_html/file.php";
$remote_file = "public_html/remote_file.php";
# FTP
$server = "ftp://ftp.yourhost.com/".$remote_file;
# FTP Credentials
$ftp_user = "ftp_username";
$ftp_password = "password";
# Upload File
$ch = curl_init();
$ftp_file = fopen($local_file, 'r');
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_USERPWD, $ftp_user.":".$ftp_password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $ftp_file);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($local_file));
$result = curl_exec($ch);
You can easily put it in a function if this does what you want.

Categories