How to copy an image from one folder to other in php - php

I have a image named xyz. Besides this file named xyz has unknown extension viz jpg,jpeg, png, gif etc. I want to copy this file from one folder named advertisers/images to other folder publishers/images in my website cpanl. How to do this with php. Thanks in advance.

You can use copy function:
$srcfile = 'source_path/xyz.jpg';
$dstfile = 'destination_path/xyz.jpg';
copy($srcfile, $dstfile);

You should write your code same as below:
<?php
$imagePath = "../Images/somepic.jpg";
$newPath = "../Uploads/";
$ext = '.jpg';
$newName = $newPath."a".$ext;
$copied = copy($imagePath , $newName);
if ((!$copied))
{
echo "Error : Not Copied";
}
else
{
echo "Copied Successful";
}
?>

Use copy() function
copy('advertisers/images/xyz.png','publishers/
images/xyz.png');
Change the file extension, whatever it is.
If you don't know the extension, go with the wildcard. It will give you the array of all the files matching with the wildcard.
$files = glob('advertisers/images/xyz.*');
foreach ($files as $file) {
copy($file,'publishers/images/'.$file);
}

Related

How to delete a file which have such type of extensions in php?

What I'm trying to do is, to delete files which includes only such type of extensions.
Let's say a folder Images/ contains following files,
a.jpg
b.jpeg
c.php
d.php2
c.png
So now I want to delete only those c.php,d.php2. Is there any way to do this in a single step or any ideas ?
Note: In this case, I do not know exact name of the file or extension.
Thank you in advance.
To delete specific extension files from sub directories, you can use the following function. Example:
<?php
function delete_recursively_($path,$match){
static $deleted = 0,
$dsize = 0;
$dirs = glob($path."*");
$files = glob($path.$match);
foreach($files as $file){
if(is_file($file)){
$deleted_size += filesize($file);
unlink($file);
$deleted++;
}
}
foreach($dirs as $dir){
if(is_dir($dir)){
$dir = basename($dir) . "/";
delete_recursively_($path.$dir,$match);
}
}
return "$deleted files deleted with a total size of $deleted_size bytes";
}
?>
e.g. To remove all text files you can use it as follows:
<?php echo delete_recursively_('/home/username/directory/', '.txt'); ?>

rename all the files in a folder

i have been trying to rename all the files (images) in a folder on my website but it does not work. the files are not renamed.
i have an input field for 'name' i want to use that name, add a uniqid and rename all the files.
here's the code that i am using:
<?php
if(isset($_POST['submit2'])){
$name = $_POST['name'];
$directory = glob("../basic_images/*.*");
{
if ($file != "." && $file != "..") {
$newName = uniqid().$name;
rename($directory.$file, $directory.$newName);
}}}
?>
besides, do i really need to _Post the $name variable?
P.S. i want to rename all the files and then copy them to another folder.
You don't need to POST name
glob is return you every files in folder with path // example /basic_images/test.jpg
then you just do foreach to loop over files, and update its name.
$path = "../basic_images/";
$directory = glob($path,"*.*");
foreach($directory as $file){
$ext = pathinfo($file, PATHINFO_EXTENSION);
$newName = uniqid().$ext;
rename($file, $path.$newName);
}
read more about glob : http://php.net/manual/en/function.glob.php
so, i finally solved the problem. now, instead of renaming the original files and then copying them to another folder, i just create new copies of the files with new names.
This is the code final code that works for me:
if(isset($_POST['submit'])){
$path = "../posts_images/";
$files = glob("../basic_images/*.*");
foreach($files as $file){
$ext = pathinfo($file, PATHINFO_EXTENSION);
$name = $_POST['new_name'];
$pic = uniqid().$name;
$newName = $pic.'.'.$ext;
copy($file, $path.$newName);
}}
it is important to use $pic.'.'.$ext because without it the new files don't have any extension.

loop through the files in a folder in php

i have searched through the Internet and found the scrip to do this but am having some problems to read the file names.
here is the code
$dir = "folder/*";
foreach(glob($dir) as $file)
{
echo $file.'</br>';
}
this display in this format
folder/s0101.htm
folder/s0692.htm
for some reasons i want to get them in this form.
s0101.htm
s0692.htm
can anyone tell me how to do this?
Just use basename() wrapped around the $file variable.
<?php
$dir = "folder/*";
foreach(glob($dir) as $file)
{
if(!is_dir($file)) { echo basename($file)."\n";}
}
The above code ignores the directories and only gets you the filenames.
You can use pathinfo function to get file name from dir path
$dir = "folder/*";
foreach(glob($dir) as $file) {
$pathinfo = pathinfo($file);
echo $pathinfo['filename']; // as well as other data in array print_r($pathinfo);
}

Read only pdf files and get the filename of that pdf files in a directory

I need to read only pdf files in a directory and then read the filename of every files then I will use the filename to rename some txt files. I have tried using only eregi function. but it seems cannot read all I need. how to read them well?
here's my code :
$savePath ='D:/dir/';
$dir = opendir($savePath);
$filename = array();
while ($filename = readdir($dir)) {
if (eregi("\.pdf",$filename)){
$read = strtok ($filename,"."); //get the filenames
//to rename some txt files using the filenames that I get before
//$testfile is text files that I've read before
$testfile = "$read.txt";
$file = fopen($testfile,"r") or die ('cannot open file');
if (filesize($testfile)==0){}
else{
$text = fread($file,55024);
fclose($file);
echo "</br>"; echo "</br>";
}
}
More elegant:
foreach (glob("D:/dir/*.pdf") as $filename) {
// do something with $filename
}
To get the filename only:
foreach (glob("D:/dir/*.pdf") as $filename) {
$filename = basename($filename);
// do something with $filename
}
You can do this by filter file type.. following is sample code.
<?php
// directory path can be either absolute or relative
$dirPath = '.';
// open the specified directory and check if it's opened successfully
if ($handle = opendir($dirPath)) {
// keep reading the directory entries 'til the end
$i=0;
while (false !== ($file = readdir($handle))) {
$i++;
// just skip the reference to current and parent directory
if (eregi("\.jpg",$file) || eregi("\.gif",$file) || eregi("\.png",$file)){
if (is_dir("$dirPath/$file")) {
// found a directory, do something with it?
echo " [$file]<br>";
} else {
// found an ordinary file
echo $i."- $file<br>";
}
}
}
// ALWAYS remember to close what you opened
closedir($handle);
}
?>
Above is demonstrating for file type related to images you can do the same for .PDF files.
Better explained here

Uploadify File Rename

I'm using the Uploadify jQuery plugin for PHP to upload a file. One thing I am stuck on is that I need to be able to rename the file being uploaded so that I can post that information to my script that inserts data into the mysql database. Can anyone please advise on how to do this?
Thanks,
Jake
Firstly rename the filename as you want in your uploadify.php
Then you just need to return $targetPath from your uploadify.php file, like this --
echo $targetPath
no need to use echo '1'
And now you have to get the renamed file name. You can get this in onUploadSuccess function.
onUploadSuccess() takes three parameters (file, data, response),
where file gives you the actual name of file you browsed through your computer, and data gives you the renamed file name which you generated through your uploadify.php code as per your requirement.
So you can try the below code --
'onUploadSuccess' : function(file, data, response) {
alert('Renamed file name is - ' + data);
}
I hope this will help out. One of my friend told me this, and I got my work done then :)
you can do like this :-
$targetFolder = FCPATH.'/resources/images/users/temp/'; // Relative to the root
if (!empty($image))
{
$time=strtotime("now");
$image['Filedata']['name']=$time.'.jpg';
$tempFile = $image['Filedata']['tmp_name'];
$targetPath = $targetFolder.$image['Filedata']['name'];
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($image['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes))
{
move_uploaded_file($tempFile,$targetPath);
echo '1';
}
else
{
echo 'Invalid file type.';
}
}
If you use uploadify.php just go right before the function move_uploaded_files and change the target name.
Anyways you do it this should work. Post the code you have if you want a more detailed answer.
This will put file in new folder with same file name as source file
$source = $_FILES['Filedata']['tmp_name'];
$filename = $_FILES['Filedata']['name'];
$newPath = $folder.'/'.$filename;
rename($source, $newPath);
/*----------------------*/
to have a new filename
function getExtension($path)
{
$result = substr(strtolower(strrchr($path, '.')), 1);
$result = preg_replace('/^([a-zA-Z]+)[^a-zA-Z].*/', '$1', $result);
if ($result === 'jpeg' || empty($result) === true) {
$result = 'jpg';
}
return $result;
}
$source = $_FILES['Filedata']['tmp_name'];
$filename = $_FILES['Filedata']['name'];
$newfileName=$folder."/"."abc".getExtension($filename);
rename($source, $newfileName);

Categories