I have issues with handling big CSV files in PHP - php

I'm using JSON to handle csv files but when the files are too big my PC starts having problems so I'm asking for a small piece of code on how to read the CSV file and insert it into the database (mysql) line by line without overwhelming the memory

You can use this code and open a stream to read a file:
$newfilename = "redirect.csv";
move_uploaded_file($_FILES["csvfile"]["tmp_name"], "img/" . $newfilename);
$file = fopen($filename,"r");
while(($entry = fgetcsv($file, 50000, ";")) !== FALSE)
{
//your code here
}

Related

Can not read Xlsx file using fgetscsv?

I have been trying to read the Xlsx file using fgetcsv concept. Its working for csv, but not for xlsx file
Thing is , its reading the file as like
Array
(
[0] => S����b��wIp� �[�������׀�4�c�W�s�c3�Y�i����Ѐ�& �g���ńA�8���'qt��]�%]>XC�<=�_���G%�����*
)
The website encountered an unexpected error. Please try again later.
and this is my code,
$file = fopen($filePath, 'r');
if($file){
fgetcsv($file, 100000, ",");
while (($example = fgetcsv($file, 100000, ",")) !== FALSE){
echo '<pre>'; print_r($exampe);
}
}
I don't want to use PHPEXCEL library file to read xlsx file, How could I solve above issue.
fgetcsv function is used to read the CSV files only you can't use this for XLS files.

PHP - Base64 Encode big files and write back synchronously

I need to base64 encode big file with PHP.
file() and file_get_contents() are not options since them loads whole file into memory.
I got idea to use this:
$handle = #fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
fclose($handle);
}
SOURCE: Read and parse contents of very large file
This working well for reading, but is it possible to do it like this:
Read line -> base64 encode -> write back to file
And then repeat for each line in file.
Would be nice if it could do it directly, without need to write to temporary file.
Base64 encodes 3 bytes of raw data into 4 bytes on 7-bit safe text. If you feed it less than 3 bytes padding will occur, and you can't have that happen in the middle of the string. However, so long as you're dealing in multiples of 3 you're golden, sooo:
$base_unit = 4096;
$handle = #fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (($buffer = fread($handle, $base_unit*3)) !== false) {
echo base64_encode($buffer);
}
fclose($handle);
}
http://en.wikipedia.org/wiki/Base64#Examples

How to download images on from server to another server?

I need to make CSV file upload for eshop. I have goods in CSV files. They have images defined for example: http://www.servername/pictures/pic1.jpg
I am finding any function, I need that script upload a CSV (solved), open CSV and explode by ",[coma]" (solved), the script go to the link with image [another server], download it and save it into directory at my server.
Colleges helped me, that I should use get_file_contents() function, but on php.net in manual I find another things. How can I solve this problem?
Please try this
if (($handle = fopen("upload/myCSV.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$imageUrl = $data[1];
$contents = file_get_contents(trim($imageUrl));
if ($contents) {
file_put_contents('/path/to/save/pic.jpg', $contents);
}
fclose($handle);
}
In myCSV.csv file data is
"1","http://imagesus.homeaway.com/mda01/5fe39690-1cbf-469d-8525-b946ad1f4ba7.1.10"
The basic procedure is:
// get the image data
$image = file_get_contents('http://www.servername/pictures/pic1.jpg');
// write the image data
$fp = fopen('path_to_your_image_folder/pic1.jpg', 'w'); //not URL
fwrite($fp, $image);
fclose($fp);
You will probably have to add some validation checks to check if the folder is writable, if there is content in $image and so on.

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

getimagesize() on stream instead of string

I'm using Valum's file uploader to upload images with AJAX. This script submits the file to my server in a way that I don't fully understand, so it's probably best to explain by showing my server-side code:
$pathToFile = $path . $filename;
//Here I get a file not found error, because the file is not yet at this address
getimagesize($pathToFile);
$input = fopen('php://input', 'r');
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
//Here I get a string expected, resource given error
getimagesize($input);
fclose($input);
$target = fopen($pathToFile, 'w');
fseek($temp, 0, SEEK_SET);
//Here I get a file not found error, because the image is not at the $target yet
getimagesize($pathToFile);
stream_copy_to_stream($temp, $target);
fclose($target);
//Here it works, because the image is at the desired location so I'm able to access it with $pathToFile. However, the (potentially) malicious file is already in my server.
getimagesize($pathToFile);
The problem is that I want to perform some file validation here, using getimagesize(). getimagesize only supports a string, and I only have resources available, which result in the error: getimagesize expects a string, resource given.
It does work when I perform getimagesize($pathTofile) at the end of the script, but then the image is already uploaded and the damage could already have been done. Doing this and performing the check afterwards and then maybe deleting te file seems like bad practice to me.
The only thing thats in $_REQUEST is the filename, which i use for the var $pathToFile. $_FILES is empty.
How can I perform file validation on streams?
EDIT:
the solution is to first place the file in a temporary directory, and perform the validation on the temporary file before copying it to the destination directory.
// Store the file in tmp dir, to validate it before storing it in destination dir
$input = fopen('php://input', 'r');
$tmpPath = tempnam(sys_get_temp_dir(), 'upl'); // upl is 3-letter prefix for upload
$tmpStream = fopen($tmpPath, 'w'); // For writing it to tmp dir
stream_copy_to_stream($input, $tmpStream);
fclose($input);
fclose($tmpStream);
// Store the file in destination dir, after validation
$pathToFile = $path . $filename;
$destination = fopen($pathToFile, 'w');
$tmpStream = fopen($tmpPath, 'r'); // For reading it from tmp dir
stream_copy_to_stream($tmpStream, $destination);
fclose($destination);
fclose($tmpStream);
PHP 5.4 now supports getimagesizefromstring
See the docs:
http://php.net/manual/pt_BR/function.getimagesizefromstring.php
You could try:
$input = fopen('php://input', 'r');
$string = stream_get_contents($input);
fclose($input);
getimagesizefromstring($string);
Instead of using tmpfile() you could make use of tempnam() and sys_get_temp_dir() to create a temporary path.
Then use fopen() to get a handle to it, copy over the stream.
Then you've got a string and a handle for the operations you need to do.
//Copy PHP's input stream data into a temporary file
$inputStream = fopen('php://input', 'r');
$tempDir = sys_get_temp_dir();
$tempExtension = '.upload';
$tempFile = tempnam($tempDir, $tempExtension);
$tempStream = fopen($tempFile, "w");
$realSize = stream_copy_to_stream($inputStream, $tempStream);
fclose($tempStream);
getimagesize($tempFile);

Categories