rename all the image files in a specified directory - php

i am trying to create couple of lines of code which will rename all the jpg images in a specified directory.The parent directory was c:/xampp/htdocs/practice/haha/.But renamed images are saved in c:/xampp/htdocs/practice/
My code do rename the file ,but problem is it deletes all the image file form the specied directory leaving the directory empty.It stores the renamed file in root php directory.And the newly created files are not clickable image file.So there are two problem i want to solve.
how can i keep the renamed files in the same directory where they were before any renaming occur.
2.How i can sustain them to be a clickable image file?
this is how they looked after renaming:
$dir='c:/xampp/htdocs/practice/haha/';
echo getcwd().'</br>';
$i=1;
if(is_dir($dir)){
echo dirname($dir);
$file=opendir($dir);
while(($data=readdir($file))!==false){
$info=pathinfo($data,PATHINFO_EXTENSION);
if($info=='jpg'){
//echo pathinfo($data,PATHINFO_BASENAME).'</br>';
echo basename(pathinfo($data,PATHINFO_BASENAME),'.jpg').'</br>';
rename($dir.'/'.$data,'image'.$i.'jpg');
$i++;
}
}
}

You missed a dot in the file name:
rename($dir.'/'.$data,'image'.$i.'jpg');
This is why they are not clickable. Use this instead:
rename($dir . '/' . $data, 'image' . $i . '.jpg');
// --------------------------------------- ^

Related

Move bulk files from one folder to the folders associated with the files

I got a csv file, that looks like the following image :
Now there are pdf files in a dir(resources/pdfFiles) that match the Number columns (1,2,3 etc) of that csv file. Now what I am trying to do is, when a button is triggered, all those pdf files will be moved to another folder. But I have a twist here,The Name column of that csv file are the names of subfolders where the associate numbered files will be stored. For example,pdf files from 1-8 matches with Akaar IT Ltd, therefore these files (1-8) will be moved to the folder called Akaar It Ltd. If there is no such folder exists, then it will be generated automatically.
My code so far are :
$path = resource_path('pdfFiles');
$files = \File::files($path);
foreach($files as $key => $file) {
$filename = basename($files[$key]);
$destinationFilePath = public_path('upload/'.$filename );
rename($files[$key], $destinationFilePath);
}
dd("probably done");
What it's doing is moving all files from one folder to another.

Get file from temp after confirm with PHP/Laravel

I have a form with a file to uplaod. All works find. But I don't want to move the file directly into a folder.
After submit I show a confirm page and there I show the uploaded file with
header('Content-Type: image/x-png');
$file = file_get_contents(\Illuminate\Support\Facades\Input::file('restImg'));
$imgType = \Illuminate\Support\Facades\Input::file('restImg')->guessClientExtension();
echo sprintf('<img src="data:image/png;base64,%s" style="max-height: 200px"/>', base64_encode($file));
This works fine. After the confirmation I like to move the file to a folder. How can I move the file after the confirmation? The Input::get('file') is not available anymore.
You will have to store the file in the initial upload somewhere temporarily other than the default tmp directory.
The documentation for PHP file uploads says:
The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed
This means that moving onto the next request, the file will no longer be available.
Instead, move it to your own custom temp directory or rename it to something special, then keep the filename in the $_SESSION to persist it to the next request.
For Laravel, this should mean putting it in the /storage directory with something like this:
// Get the uploaded file
$file = app('request')->file('myfile');
// Build the new destination
$destination = storage_path() . DIRECTORY_SEPARATOR . 'myfolder';
// Make a semi-random file name to try to avoid conflicts (you can tweak this)
$extension = $file->getClientOriginalExtension();
$newFilename = md5($file->getClientOriginalName() . microtime()).'.'.$extension;
// Move the tmp file to new destination
app('request')->file('myfile')->move($destination, $newFilename);
// Remember the last uploaded file path at new destination
app('session')->put('uploaded_file', $destination.DIRECTORY_SEPARATOR.$newFilename);
Just remember to unlink() the file after the second request or do something else with it, or that folder will fill up fast.
Additional Reference:
http://api.symfony.com/2.7/Symfony/Component/HttpFoundation/File/UploadedFile.html

phpqrcode - save file to temporary directory

I'm trying to create a QR-code with the php library phpqrcode.
I want to save the file in a temporary file so I can use it afterwards. Something like this.
I'm using the Zend Framework and want to use the temporary directory. This is what I have so far:
require_once 'phpqrcode/qrlib.php';
require_once 'phpqrcode/qrconfig.php';
$tempDir = '/temp';
$fileName = 'test'.'.png';
$pngAbsoluteFilePath = $tempDir . $fileName;
$urlRelativeFilePath = '/temp' . $fileName;
// generating
if (!file_exists($pngAbsoluteFilePath)) {
QRcode::png('http://mylink.com/s/'.$quiz_url, $pngAbsoluteFilePath, 'L', 4, 2);
echo 'File generated!';
echo '<hr />';
} else {
echo 'File already generated! We can use this cached file to speed up site on common codes!';
echo '<hr />';
}
echo 'Server PNG File: '.$pngAbsoluteFilePath;
echo '<hr />';
// displaying
echo '<img src="'.$urlRelativeFilePath.'" />';
My output shows:
Server PNG File: /temptest.png
And an image that can't be found. Can somebody help me on the way?
EDIT:
When I tried to change the '/temp' to '/temp/' I get this warning:
Warning: imagepng(/temp/test.png): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/surveyanyplace/site/library/phpqrcode/qrimage.php on line 43
SECOND EDIT:
When I checked my hard drive I saw that he just saves images on my root map like 'temptest.png'... How can I make sure he save this on temp folder on my server?
The image can't be found by your browser because it's looking for it at the URL "http://domain.tld/temptest.png" which relates to location "/Applications/MAMP/htdocs/surveyanyplace/site/public/temptest.png" on your disk and is not where the file was saved (it's "/temptest.png" as you noticed).
In order to directly serve files via your web server (Apache) you have to make sure the image file is under it's DocumentRoot (typically the "public" folder of your Zend Framework application)
One way is to create the following directory : "/Applications/MAMP/htdocs/surveyanyplace/site/public/qrcodecaches" and change $pngAbsoluteFilePath and $urlRelativeFilePath as follows:
$pngAbsoluteFilePath = APPLICATION_PATH . '/../public/qrcodecaches/' . $fileName;
$urlRelativeFilePath = '/qrcodecaches/' . $fileName;
($tempDir can be removed)
NB: You might want to take a look to Zend_View_Helper_BaseUrl to make $urlRelativeFilePath more portable when dealing with applications inside a subdirectory

FTP folder file copying from one directory to another directory

I am trying to copy a uploaded file which is in my directory called "upload" and is called image (5).jpg to my other directory called "images". This is my code.
<?php
copy(upload/image (5).jpg, images/);
?>
Am I doing this right?
First your image file name is awful, please do not use spaces in file names. Also do not use brackets in file name. Some systems like google app engine would easily mark that as invalid file name and ignore it.
Then adjust a little your line of code to look like this:
$old_file = 'upload/image_5.jpg';
$new_file = 'images/image_5.jpg';
$copied = copy(old_file, $new_file);
if ($copied) {
print "file " . $old_file . " is copied to " . $new_file;
} else {
print "error";
}
If you encounter an error maybe paths to files are not configured right. You do not have a write permission on that specific directory etc.

password protect directory but still allow use on main page

I'm trying to move an image to a tmp directory on the server side and then send it to the client from the tmp folder, but things aren't quite working. Here's what I currently have.
//this is where the image resides permanently
$imgdirectory = "../" . $ms . "msimages/" . $img_filename;
//here I'm trying to get the contents of the img from its permanent location
$content = file_get_contents($imgdirectory);
//here I'm trying to create a new temporary file
file_put_contents('/tmp/img.jpg', $content);
...
echo "img {background:url(\"/tmp/img.jpg\")-" . $xleft . "px -" . $ytop . "px";}"; this is the css trying to use file that is supposed to be in the temp folder
and here's the css output I get when the page loads
img#{width:631px;height:453px;background:url("/tmp/img.jpg")-144px -112px;}
But unfortunately, there is no file at /tmp/img.jpg so something must not quite be working with the file_put_contents 404 image not found
btw, I'm not getting any errors about permissions to put contents in /tmp,
Any advice is appreciated.
Use copy instead of reading and writing files
bool copy ( string $source , string $dest [, resource $context ] )
And
file_put_contents('/tmp/img.jpg', $content);
will put it under root tmp directory not in your sites tmp directory.
Try
file_put_contents($_SERVER["DOCUMENT_ROOT"] . 'tmp/img.jpg', $content);

Categories