Download multiple .csv file separately generated by PHP Script - php

I am running PHP Script using CodeIgniter Framework to generate user's and it's devices informations; Two different files are generated with help of
csv_from_result($user_query, $delimiter, $newline);
function of $this->load->dbutil();.
When i use
$this->load->helper('download');
library to download above .csv file using
force_download($user_file_name, $usercsvdata);
function it downloads first .csv file only, the second file is not downloaded which is of User's Devices.
Please help me to Solve this.
Thanks.

CodeIgniter has a Zip file library build in. In order to download multiple files at the same time, they have to be added to an archive, like a .zip file.
<?php
//.. get $usercsvdata and $devicecsvdata
$this->load->library('zip');
$this->zip->add_data('users.csv', $usercsvdata);
$this->zip->add_data('devices.csv', $devicecsvdata);
$this->zip->download($user_file_name);

Related

I want to download a proxylist with curl in php

I want to download a proxy list with this api:
https://api.proxyscrape.com?request=getproxies&proxytype=http&timeout=5000&country=US&anonymity=elite&ssl=yes
how can I do it in php with curl???
When you open this url, It will automatically download a text file with proxies.
I want to download and place it in the current directory.
The easiest way would be using file_get_contents() function built into PHP.
So to download & save the file you would do something like this:
$proxies = fopen("proxies.txt", "w");
fwrite($proxies, file_get_contents('https://api.proxyscrape.com?request=getproxies&proxytype=http&timeout=5000&country=US&anonymity=elite&ssl=yes'));
fclose($proxies);

read contents of zipped files through file_get_contents in codeigniter in .srt format and upload to server

I am trying to read contents of zipped file as
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
And trying to upload at my online server as below
$this->load->library('zip');
$data = $subda;
$name = 'myfile.srt';
$this->zip->add_data($name, $data);
$this->zip->archive('assets/subtitles/myzipfile.zip');
But when I check this uploaded file at my server it does not compressed properly.
it does not contain any data.
when I echo $subda it give results like.
Where I am wrong...
through file_get_contents I am already getting contents of zip file.
You cannot do:
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
This will just load the ZIP content into the string rather than uncompressed.
See this on how you can read using a lib in php:
Best way to read zip file in PHP

Generating text files

My apologies but this is a How to Question. I have limited knowledge of PHP and need figure out if PHP can do this
I have a list of video files that I need tagged with xspf files. And there are thousands of them all in the same directory!
How to make a PHP script read the video file names and create a text file with the video file name inside the text file with other pre-created text and then save the text file the same name as the video file(not the extension)
I searched and could only find a mp3 xspf generator but it creates one playlist of all the mp3 files it found in one directory.. I need to create a xspf file for each video(MP4) file in a directory - http://sourceforge.net/projects/php-xspf-gen/
Or if anyone knows of an windows App that can do the same...
Thank you..
I don't have any experience with xspf files, but creating a file for each video file is pretty easy.
$videoDir = 'C:/path/to/video';
$textDir = 'C:/path/to/textfiles';
foreach(scandir($videoDir) as $filename){
file_put_contents($textDir.'/'.$filename.'.txt', 'Text to put in file');
}
Some additional Code will be needed, but I hope this helps you in some way.

Clean up after creating the pdf

I'm generating a pdf file with html2fpdf.
$pdf = new HTML2FPDF();
$pdf->HTML2FPDF("P","mm","A4");
$pdf->AddPage();
$pdf->WriteHTML($html);
$pdf->output('sample.pdf');
This sample works great. But:
How do I delete the pdf after the output? I just want to have links in my tool, the users can download the pdf and after that it shoud be deleted on the server.
How can I 'clean up' after generating the pdf?
You can use PHP's file deletion function called unlink()
Call this function with the full path to the generated PDF file (or any file for that matter) and PHP will delete that file.
http://php.net/manual/en/function.unlink.php
You don't necessarily have to delete the file immediately after the user has downloaded it. You can just as easily place all the generated files in one central folder and have a cron job execute a more general clean up script simply removing the older files.
One method could be -
Scan the contents of the folder using scandir().
Iterate over its files in a foreach loop..
Inspect the creation time of each file using filemtime().
If the creation time was over hour ago, delete the file using unlink().
Because you are generating the PDF file yourself within your PHP code, I didn't mention the permissions consideration. Here would be a good place to mention that your PHP must have the correct file system permissions in order to perform any action on the file system. You are creating a PDF file so it's safe to assume that you have the correct permissions to make changes to the file system but if you plan on using this unlink() function in other scripts make sure that the files you are dealing with have the correct permissions set.
If you don't add the 'F' flag to the output function there will be no pdf files stored on the server at all:
$pdf->output('sample.pdf', 'F'); //stores PDF on server
In your case the script itself behaves like an actual pdf file. So, creating a link to the script is just like a link to the pdf, except that the PDF is created every time the script is requested. To tell the browser it's a PDF the content-type response header must be set to application/pdf:
content-type: application/pdf
This way the broser knows that it's a pdf even if the URL is ending in a .php. You can use rewrite engine to make it end in pdf or whatever else.
Sending the headers is done by the fpdf/tcpdf. In short: you don't have to do any cleanup, because no pdf file is stored on the server.
If you wonder what the name is for than, try saving the pdf file. The recommanded name when saving will be sample.pdf.
Reference:
PHP header() function, at the examples there is one for sending pdf
FPDF::Output()
TCPDF::Output()

Downloading images from a server iPhone SDK

I have created a program which allows me to upload images to my server. They are given a random file name when uploaded. I want to be able to download all the images from a folder on the server so I can display them in my application. The only example I have seen requires that I know the file name of the images which I don't. How could I download all the images in a given directory (and store the downloads in an NSArray)? If there is no native way to do it does anyone know a way that it could be done via calling a PHP script? (I use a PHP script which the iPhone calls to upload the images).
Thanks.
To list all images in a directory using PHP and return a JSON encoded string:
$path = '/full/path/to/images/';
// find all files with extension jpg, jpeg, png
// note: will not descend into sub directorates
$files = glob("{$path}/{*.jpg,*.jpeg,*.png}", GLOB_BRACE);
// output to json
echo json_encode($files);
you can call a php script that will return the url for all of your images.
something like
yoursite.com/image123.jpg;yoursite.com/image213.jpg;yoursite.com/imageabc.jpg
then you parse the result, split by ";" and get the array of urls which you need to download.

Categories