Trying to find file extension on hidden download url - php

I am trying to make a script to take a downloadable file and put it onto my ftp server to then update a database. I cannot figure out what the url extention should be to able to make this work.
<?php
$src = fopen('https://www.atf.gov/firearms/docs/0516-ffl-listxlsx/download', 'r');
$dest1 = fopen('first1k.xlsx', 'w');
$dest2 = fopen('remainder.xlsx', 'w');
echo stream_copy_to_stream($src, $dest1, 19759460) . " bytes copied to first1k.txt\n";
echo stream_copy_to_stream($src, $dest2) . " bytes copied to remainder.txt\n";
?>
This is where I am trying to get the data from https://www.atf.gov/firearms/listing-federal-firearms-licensees-ffls-2016

<?php
$file = "0516.xlsx";
$url = 'https://www.atf.gov/firearms/docs/0516-ffl-listxlsx/download' ;
$handle = fopen("0516.xlsx", "w+");
fwrite($handle, file_get_contents($url));
rewind($handle);
$read = htmlentities(fread($handle, filesize($file)));
?>
fixed it out

Related

Finding a file with PHP

I want to find a file with a wildcard in the same directory as my index.php is.
When I assign the $file_name manually with the name string, it works fine.
<?php
$file_name = glob("*.csv");
$handle = fopen($file_name, "r");
$file = fread($handle, filesize($file_name));
fclose($handle);
echo $file;
?>
The browser should output the content of the .csv file, like when I assign the $file_name manually.
you need a loop because glob() returns an array:
foreach (glob("*.csv") as $filename) {
$handle = fopen($filename, "r");
$file = fread($handle, filesize($filename));
fclose($handle);
echo $filename;
}
This script will find some images in working folder.
<?php
$workdir = getcwd(); // my working dir
$patternTofind = ".{jpg,gif,png}"; // Images example
$files = glob("$workdir*$patternTofind", GLOB_BRACE);
// Print result found
print_r($files);
?>

Could not create file Using PHP code

On the 000webhost.com server
This is the code
<?php
$my_file = fopen("my_test.txt", 'w');
fclose($my_file );
I thank those who help me in advance
Use the function chmod
http://it2.php.net/function.chmod
$file = "my_test.txt";
$fp = fopen($file, 'w');
fwrite($fp, $content);
fclose($fp);
chmod($file, 0777);
Note* fwrite is optional in your case if you need to enter some data in file

PHP bulk file read only list file in loop

php bulk file read problem in loop.
I have 3 image files which are already on server. Say it is picture1.jpg, picture2.jpg, picture3.jpg
I have this code which read the file but $thepic = fread($fh, $fs); only read last file ( picture3.jpg) in the array.
$imgfolder = "images/";
foreach ($picturefile as $pfile) {
echo $imgpath = "./". $imgfolder.$pfile; // This echo shows all files path when loop run
$picFile = $imgpath;
$fh = fopen($picFile, 'r');
$fs = filesize($picFile);
$thepic = fread($fh, $fs);
echo $thepic; // $thepic variable only show last file.
echo "<br>-----------------------------------------------<br>";
fclose($fh);
}

How to read a PDF file from web and store it in PHP?

I have PDF files on some server
$path = 'http://someserver/somepath/someFile.pdf'
I would like to save these files in my Server
$file_name = 'someFile.pdf';
$file_handler = fopen('/' . DS . $file_name, 'w');
$PDF_CONTENTS = file_get_contents($path);
file_put_contents($file_handler, $PDF_CONTENTS);
fclose($file_handler);
It is creating someFile.pdf but no contents.
No need of using fopen.
Try
$storeLocation = '/pdf/mypdf.pdf';
$PDF_CONTENTS = file_get_contents($path);
file_put_contents($storeLocation, $PDF_CONTENTS);

Fopen problems in php

In PHP im creating a tool to open txt file with an integer in it, increment the number, save the number to a variable and then save and close the file. this is the code i have for it and it doesnt seem to work when i have it on my test server. Can anyone clue me in as to why this isnt working properly?
//opens pclnumber.txt to $handle, saves number to $number, Increments number in text file, saves and closes file
$handle = fopen("pclnumber.txt", "w+");
$number = fread($handle);
fwrite($handle, $number+1);
fclose($handle);
over all you must set reading permission on your file, and than you can use the follow code:
$filename = "pclnumber.txt";
//read file content
$handle = fopen($filename, "r");
$number = fread($handle, sizeof($filename));
fclose($handle);
//update file content
$handleWrite = fopen($filename, "w+");
fwrite($handleWrite, $number+1);
fclose($handleWrite);
Bye,
Marco

Categories