Filesize stat failed while uploading files - php

I'm facing strange problem right now - "filesize(): stat failed for C:\xampp\tmp\php7A38.tmp" exception. The problem occurs when I'm uploading files in my application built with PHP (Laravel).
Before I'm uploading the files onto the server I'm checking size of files like this (this works very well):
for ($i = 0; $i < $filesLength; $i++) {
if(filesize($request['files'][$i]) < 1572865) {
$file = $request['files'][$i];
$filename = $imageNumber.'.'.$request['files'][$i]->extension();
$file = $file->move(public_path().'/app/newsimages/'.$element->id.'/', $filename);
}
}
If I do it like that everything works very well. But the problem is that I have to put this loop in another loop, like this:
foreach($somelement as $element) {
for ($i = 0; $i < $filesLength; $i++) {
if(filesize($request['files'][$i]) < 1572865) {
$file = $request['files'][$i];
$filename = $imageNumber.'.'.$request['files'][$i]->extension();
$file = $file->move(public_path().'/app/newsimages/'.$element->id.'/', $filename);
}
}
}
In addition to that it crashes at the second loop of the foreach loop.
Maybe you have some idea what's wrong in here?

I think this is obvious, in inner loop you move the file, so when you go to next iteration of outer loop file is not there, so for example if you once move the file:
$request['files'][0]
it's not possible to execute:
filesize($request['files'][0])
because this file was moved - it doesn't exist any more.

Related

exif_read_data() returning false even though there is metadata present in jpeg

For some reason exif_read_data() returns false on every image in the directory even though I know that all my jpeg images have metadata properties.
I am by no means a PHP wizard yet so perhaps I have a syntax error or I'm just missing something fairly obvious to those of you who are PHP wizards.
galleryData.metadata = <?php
$dir_path = "Assets/Images/portfolio/";
if (is_dir($dir_path)) {
$files = scandir($dir_path);
for ($i = 0; $i < count($files); $i++) {
$tempPath = $dir_path + $files[$i];
$metadata[$i] = exif_read_data($tempPath);
}
echo json_encode($metadata);
}
?>;
Thankfully figured it out I wasn't combining my strings properly. Below is my fixed and working code.
galleryData.metadata = <?php
$metadata = array();
if (is_dir($dir_path)) {
for ($i = 0; $i < count($files); $i++) {
$metadata[$i] = exif_read_data("{$dir_path}{$files[$i]}", null, true);
}
echo json_encode($metadata);
}
?>;
might be a solution, but need to have more info real error (error message ?)
exif_read_data() can be buggy prom php version to version:
Bug #75785 Many errors from exif_read_data
The solution might be to use
$img = new \Imagick(DSC01386.jpg);
$allProp = $img->getImageProperties();
$exifProp = $img->getImageProperties("exif:*");
The Imagick class, is quite powerful class (rotations etc).
The full story for that solution here

Delete files from zip prior to extracting using PHP

I am using PHP to upload a zip file and extracting it. I was having problems with my validation script, and just discovered that the culprit was Thumbs.db files which windows creates to improve caching. How can I delete these files prior to extracting?
Below is my attempt, but when extracting, other files have also been deleted. I've tried using both deleteIndex() and deleteName(), but get the same results. If I comment out the the line which deletes the Thumbs.db files, I do not experience the unintended deleted files. My syslog() line only indicates that the Thumbs.db files are being deleted.
<?php
$zip = new ZipArchive();
if ($zip->open($_FILES['upload_file']['tmp_name']) === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
//Validate files
$name=$zip->getNameIndex($i);
$pieces=explode(DIRECTORY_SEPARATOR,$name);
if($pieces[count($pieces)-1]=='Thumbs.db'){
//Delete windows created thumb.db files
syslog(LOG_INFO,'delete file '.$name.' with index '.$i);
$zip->deleteIndex($i); //If commented out, I do not experience the wrong files being deleted
//Also deletes wrong files: $zip->deleteName($name);
}
}
$zip->extractTo($myPath);
}
?>
Close the archive after file manipulations and reopen it before extracting:
if ($zip->open($_FILES['upload_file']['tmp_name']) === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
...
}
$zip->close();
if ($zip->open($_FILES['upload_file']['tmp_name']) === true) {
$zip->extractTo($myPath);
}
}
I believe in your example $zip->extractTo($myPath); return false.
In your particular case it may be simpler to use system utilities to unpack the archive and then delete the files with find.

PHP mass file copy

If I have a file called file.html how do i make 10 clones of this file via PHP such that they are renamed file1....file10?
$filename = 'file.html'
$copyname = 'file2.html'
if ($file = #fopen($copyname, 'x')) {
// We've successfully created a file, so it's ours. We'll close
// our handle.
if (!#fclose($file)) {
// There was some problem with our file handle.
return false;
}
// Now we copy over the file we created.
if (!#copy($filename, $copyname)) {
// The copy failed, even though we own the file, so we'll clean
// up by itrying to remove the file and report failure.
unlink($copyname);
return false;
}
return true;
}
Small file approach: this allows you to do something with the contents of the file before saving it:
$text = file_get_contents('file.html');
for($i = 0; $i < 100; $i++) {
file_put_contents('file'.$i.'.html', $data);
}
Bigger files approach: this does not allow you to access the contents of the file before saving it, it only tells the underlying OS to make the copy (equivalent to a linux bash command of cp file.html file1.html):
for($i = 0; $i < 100; $i++) {
copy('file.html', 'file'.$i.'.html');
}
Just run your code in a loop:
$filename = 'file.html'
for($i=1; $i<=10; $i++) {
$copyname = "file$i.html";
copy($filename, $copyname);
}
Feel free to add error checking and handling.

Show All Folders From FTP - PHP Keeps Looping :: Error ::

I am trying to get a list of dir from a ftp using php the following code is outputting the following information.
httpdocs/user_images
httpdocs/user_images/inc
httpdocs/user_images/inc/smarty
httpdocs/user_images/header
httpdocs/user_images/header/logo80.jpg
httpdocs/user_images/header/logo80.jpg
httpdocs/user_images/header/logo80.jpg
httpdocs/user_images/header/logo80.jpg
It keeps on repeating the follow httpdocs/user_images/header/logo80.jpg over 60 times.
Here is my code
function ListOfFolder($folder_listarry,$conn_id){
for ($i=0; $i<sizeof($folder_listarry); $i++) {
echo $folder_listarry[$i]."<br>";
$contents = ftp_nlist($conn_id, $folder_listarry[$i]);
ListOfFolder($contents,$conn_id);
}
}
$contents = ftp_nlist($conn_id, "httpdocs/");
ListOfFolder($contents,$conn_id);
I an not sure it is a guess only. you can try by modifying your function with is_dir
function ListOfFolder($folder_listarry,$conn_id){
for ($i=0; $i<sizeof($folder_listarry); $i++) {
echo $folder_listarry[$i]."<br>";
if (is_dir($folder_listarry[$i]) === false)
{
continue;
}
$contents = ftp_nlist($conn_id, $folder_listarry[$i]);
ListOfFolder($contents,$conn_id);
}
}
ftp_nlist will also return
.
and
..
for same and parent dir.
You need to exlude them.

How to get ZipArchive to not overwrite certain files and folders

I would like to extract a zip folder to a location and to replace all files and folders except a few, how can I do this?
I currently do the following.
$backup = realpath('./backup/backup.zip');
$zip = new ZipArchive();
if ($zip->open("$backup", ZIPARCHIVE::OVERWRITE) !== TRUE) {
die ('Could not open archive');
}
$zip->extractTo('minus/');
$zip->close();
How can I put conditions in for what files and folders should NOT be replaced? It would be great if some sort of loop could be used.
Thanks all for any help
You could do something like this, I tested it and it works for me:
// make a list of all the files in the archive
$entries = array();
for ($idx = 0; $idx < $zip->numFiles; $idx++) {
$entries[] = $zip->getNameIndex($idx);
}
// remove $entries for the files you don't want to overwrite
// only extract the remaining $entries
$zip->extractTo('minus/', $entries);
This solution is based on the numFiles property and the getNameIndex method, and it works even when the archive is structured into subfolders (the entries will look like /folder/subfolder/file.ext).
Also, the extractTo method takes a second optional paramer that holds the list of files to be extracted.
If you just want to extract specific files from the archive (and you know what they are) then use the second parameter (entries).
$zip->extractTo('minus/', array('file1.ext', 'newfile2.xml'));
If you want to extract all the files that do not exist, then you can try one of the following:
$files = array();
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
// if $filename not in destination / or whatever the logic is then
$files[] = $filename;
}
$zip->extractTo($path, $files);
$zip->close();
You can also use $zip->getStream( $filename ) to read a stream that you then write to the destination file.

Categories