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.
Related
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
}
I'm trying to read only colum with red label in a csv file. Is there a php function to do this or a symfony bundle?
Now I'm reading csv file with fgetcsv function:
foreach($request->files->get('importFile') as $file) {
if (($handle = fopen($file->getRealPath(), "r")) !== FALSE) {
// get the rest of the rows
$data = array();
$i = 0;
while ($row = fgetcsv($handle, 0, ';')) {
if($i>1) {
$data[] = $row;
$i++;
}
print_r($data);die;
}
}
}
But it doesn't read the label's color.Is there a way to read the color on the csv files?
CSV files have no formatting.
.xls or .odt files have formatting, but CSV definitely not - only data are saved in there. Look at the file with a text editor.
You can use this php class to read csv files: https://git.webworks-nuernberg.de/webworks-nuernberg/parsecsv
But cweiske is right, csv hasn't any formatting.
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
there are two columns in my csv file,eg:
image gallery
/1.jpg /a.jpg;/b.jpg
..... .....
now i want to update the gallery content to /1.jpg;/a.jpg;/b.jpg. namely,add the content of image collumn and ; to the gallery content.
the following is my code.when i run it. it can't update the content of the csv.i am get stucked.
$dir = getcwd();
$files = scandir($dir);
foreach ($files as $file) {
$parts = pathinfo($file);
if ($parts['extension']!="csv") {
continue;
}
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 4096, ",")) !== FALSE) {
$data[1]=$data[0].";".$data[1];
fputcsv($file, $data);
}
fclose($handle);
}
open file in write or append mode and
fputcsv expects first parameter to be resource and you have given file path
which is causing problem
change it
fputcsv($handle, $data);
Please check the file permission first and after that you have to change the handle to both read and write and also please check whether the data[1] is having the values.
Because in Your code the the data[0] only will fetch the lines as a string which is separated with ";" so you have to explode it and after that do the operations.
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);