copy files from remote url of same extension different names - php

Hi I have successfully copied file from remote url to mine using copy
if(!#copy('http://www.example.com/download/somedoc.pdf','./pdf/somedoc.pdf'))
{
$errors= error_get_last();
echo "COPY ERROR: ".$errors['type'];
echo "<br />\n".$errors['message'];
} else {
echo "File copied from remote!";
}
But I need to do it manually and one at a time. I did Google for * file name with same extension like somedoc.pdf. anotherdoc.pdf, next.pdf.
Is'nt there anyway to copy automatically to my local server from remote url that is newly uploaded
Found My Ans:
yeah got it.. used simplehtmldom.. The html code or www.example.com was
<a target="_blank" style="text-decoration:none" href="./images/download/somedoc.pdf">somedoc and somename</a>
and my code was
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://www.example.com');
// find all link
foreach($html->find('a[target=_blank]') as $e)
echo $e->href . '<br>';
http://simplehtmldom.sourceforge.net/
now i will convert the output to variable and use the copy function
Thanks for the reponses guys..

Related

download file to my server with php with a url without file name

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

Read an excel file stored in media folder

I'm developing a WordPress plugin that requires to upload an excel file and then read the data inside of it.
I'm using the function "media_handle_upload" to upload the excel file, then, when I try to use SpreadsheetReader to read the file there's an error saying that the file is not readable.
This is what I'm coding, (I know it's horrible coding, but I'm learning and this is just to get the plugin working)
if(isset($_FILES['test_upload_pdf'])){
$pdf = $_FILES['test_upload_pdf'];
// Use the wordpress function to upload
// test_upload_pdf corresponds to the position in the $_FILES array
// 0 means the content is not associated with any other posts
$uploaded=media_handle_upload('test_upload_pdf', 0);
// Error checking using WP functions
if(is_wp_error($uploaded)){
echo "Error uploading file: " . $uploaded->get_error_message();
}else{
echo $pdf."<br>";
$year = date('Y');
$month = date('m');
$targetPath = get_site_url()."/wp-content/uploads/".$year."/".$month."/".$_FILES['test_upload_pdf']['name'];
$targetPath2 = $_SERVER['DOCUMENT_ROOT']."/".$_SERVER["HTTP_HOST"]."/wp-content/uploads/".$year."/".$month."/".$_FILES['test_upload_pdf']['name'];
echo "<br>";
echo $targetPath2;
echo "<br>";
try
{
$Reader = new SpreadsheetReader($targetPath2);
}
catch (Exception $E)
{
echo $E -> getMessage();
}
I think the reader doesn't work because is trying to access "localhost" instead of the physical folder, for example, one of the $targetPath prints this:
http://127.0.0.1/wordpress/wp-content/uploads/2019/04/example.xlsx
So... My question is, there's a way to access the media files so I can open them with the reader?
Thanks.
Perhaps this helps:
https://developer.wordpress.org/reference/functions/wp_upload_dir/
Otherwise try to hardcode folder "/your/path/to/upload/folder/file.extension" and find out how to create this programmaticly

CKEditor issue with drag and drop image upload

I'm having some difficulties configuring uploadimage plugin on CKeditor to permit drag and drop of images.
I included both plugins uploadimage and it's dependent uploadwidget.
This is the upload php file:
$basePath = "/var/www/html/images/articles/";
$baseUrl = "/var/www/html/images/articles/";
$funcNum = $_REQUEST['CKEditorFuncNum'] ;
if (isset($_FILES['upload'])) {
$name = $_FILES['upload']['name'];
move_uploaded_file($_FILES["upload"]["tmp_name"], $basePath . $name);
$url = $baseUrl . $name ;
$message = 'new file uploaded';
}
else
{
$message = 'No file has been sent';
}
echo "<script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction($funcNum, '$url', '$message')</script>";
The problem is with CKEditorFuncNum, since I'm not receiving anything from the form.
File is correctly uploaded into the $basePath but the function window.parent.CKEDITOR.tools.callFunction fail due to the missing funcNum
ckeditor.js:3 [CKEDITOR] Error code: filetools-response-error.
Object {responseText: "<br />↵<b>Notice</b>: Undefined index: CKEditorFu…icles/371c63d.jpg', 'new file uploaded')</script>"}
Do you guys know why I'm not getting anything from $_REQUEST['CKEditorFuncNum'] ?
THanks
Have you checked docs on File Browser API describing creation of custom file manager?
As for CKEditorFuncNum, I would check if it is a problem with CKEditor or PHP code. Just check the upload request in the browser dev console to see if CKEditor adds CKEditorFuncNum param:
And then depending on the cause you may try to dive deeper.

Wordpress : Guetzli upload prefilter plugin

I'm trying to make my first WordPress plugins, and i have a issue.
I want to make a image compressor on upload in WordPress. I use the prefilter option to work on image before the upload is done with WordPress native upload function.
The fact is that the image upload, but without being compressed. But all seems good, and when I take the request sent and copy it in my computer's terminal, it works.
The server I use for now is my localhost, powered by xampp. Another similar function with pngquant works properly, so I don't think it's a server issue.
Here's my code :
function compressed_image_upload($file){
$ext = pathinfo($file['name'],PATHINFO_EXTENSION);
/*Use of guetzli*/
$allowed = array("jpg", "jpeg");
if(in_array($ext,$allowed)) {
echo "<pre>";
var_dump($file);
echo "</pre>";
$guetzli_path = get_home_path()."guetzli"; /*Preparation of guetzli command*/
if(is_executable($guetzli_path)) {
echo "guetzli executable : yes \n";
$request = $guetzli_path." ".$file['tmp_name']." ".$file['tmp_name'];
exec($request, $output, $return); /*Execution of guetzli*/
echo "<pre> I'm there ! \n";
echo "request : $request \n";
echo "output : $output \n";
echo "return : $return \n";
echo "</pre>";
}
}
return $file;
}
register_activation_hook( __FILE__, 'compressed_image_upload' );
And there is the results. As I said earlier, my image is upload, but as if the exec line did nothing.
result of this part of code
Any idea ? (First post on slackoverflow, be merciful if this question is not clear enough ^^. I'd gladly add informations if its lacks).
nb : guetzli works like this : "executable_path input_file_path output_file_path". In my case, i want it to erase the temporary file so it can be used by the next step of upload.

PHP wamp using relative path failed to write to text document

kindly help me.
my env as follow : window 7 x64,wamp x64 v2.5.
apache web root directory : D:/wampProject/.
I want to write to the target text file in D:/wampProject/chapter02/orders.txt
syntaxTest.php in D:/wampProject/,as below.
<?php
header("Content-type: text/html; charset=utf-8");
$document_root= $_SERVER['DOCUMENT_ROOT'];
//#1 $filename = "$document_root"."chapter02/orders.txt"; //works
//#2 $filename="D:/wampProject/"."chapter02/orders.txt"; //works
//#3 $filename="$document_root../orders.txt";
//#3 output: filename D:/wampProject/../orders.txt
//showed contents added in the page after refresh,
//but text file didn't get written refer pics below .
//Got the wrong dir? seems like page result shows 'cache' written content
//added by absolute path in all time by the script.
//#4 $filename="D:/wampProject/"."../orders.txt";
//filenameD:/wampProject/../orders.txt as failed as #3.
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
} ;
echo '<br/>';
if (is_readable($filename)) {
echo 'The file is readable';
} else {
echo 'The file is not readable';
};
$fp=fopen($filename,'ab');
$outputstring="address string"."\n";
echo $outputstring.'<br />';
flock($fp,LOCK_EX);
fwrite($fp,$outputstring,strlen($outputstring));
flock($fp,LOCK_UN);
fclose($fp);
echo "fclosed".'<br />';
$filecon=file_get_contents($filename);
echo $filecon."<br />";
?>
page result
orders.txt after using 4 filename paths above

Categories