copy images issue in php - php

I want to copy some images from one folder to another, I do not want to copy them all.
Here is my code but it give me an issue failed to open stream
$image = "a.jpg";
$srcfile='uploads/listings/my_from/'.$image;
$dstfile='uploads/listings/my_to/images/';
copy($srcfile, $dstfile);
What am I missing? Can I do anyhting else so that I can copy selected images to destination without deleting it?
Note: both of these folders are on the same server and same project. Should I do it by curl?

It might pay to do some error checking as well, but you just need to add the full image path to your $dstfile variable:
<?php
$image = "a.jpg";
$srcfile='uploads/listings/my_from/'.$image;
$dstfile='uploads/listings/my_to/images/'.$image;
echo 'Attempting to copy "'.$srcfile.'" to "'.$dstfile.'"<br />';
if(file_exists($srcfile)) {
if(file_exists($dstfile)) {
echo 'Cannot copy file, destination file already exists';
} else {
if(is_writable(dirname($dstfile))) {
if(copy($srcfile,$dstfile)) {
echo 'File successfully copied';
} else {
echo 'File could not be copied';
}
} else {
echo 'Destination is not writable';
}
}
} else {
echo 'Cannot copy file, source file doesnt exist';
}
?>

if you are sure that file a.jpg exists, then this should work
$srcfile='uploads/listings/my_from/'.$image;
$dstfile='uploads/listings/my_to/images/'.$image;
copy($srcfile, $dstfile);
note: destination file name must be specified.

You should check it exists really.
try:
$image = "a.jpg";
$srcfile='uploads/listings/my_from/'. $image;
$dstfile='uploads/listings/my_to/images/' . $image;
if(!file_exists($srcfile) {
throw new \Exception("File does not exist!");
}
copy($srcfile, $dstfile);

Related

PHP unlink removes file, but file still exists

I have a very simple function:
unlink($oldPicture);
if (is_readable($oldPicture)) {
echo 'The file is readable';
} else {
echo 'The file is not readable';
}
}
The file shows not readable after execution and disappears from the file directory. However, it's still available when accessed from the browser despite not being cached (opened the file on separate browsers for testing). Is there something I am missing here? Is the file being cached by the server? That is the only explanation I could come up with.
Try something like:
if (is_file($oldPicture)) {
chmod($oldPicture, 0777);
if (unlink($oldPicture)) {
echo 'File deleted';
} else {
echo 'Can\'t remove file';
}
} else {
echo 'File does not exist';
}
Make sure you have full path for $oldPicture
Example:
$oldPicture = dirname(__FILE__) . '/oldpicture.png';

delete image in folder and his ''mini'' size in same time

I've made a folder "../img/travaux/villa" that contain my image, so when i upload image i made a script that create a "mini" copy of this image inside a new folder "../img/travaux/villa/mini", with the same name.
now i want make a button for delete my image, but i also want that delete the "mini" image in the same time how can i make it ?
this is my code for show and delete the image, but that doesn't delete the mini image:
<?php
if (array_key_exists('delete_file', $_POST)) {
$filename = $_POST['delete_file'];
if (file_exists($filename)) {
unlink($filename);
echo 'File '.$filename.' has been deleted';
} else {
echo 'Could not delete '.$filename.', file does not exist';
}
}
$folder = glob("../img/travaux/villa/*jpg");
foreach ($folder as $picture) {
echo "<div class='divimages'>";
echo '<img src="'.$picture.'"/>';
echo '<form method="post">';
echo '<input type="hidden" value="'.$picture.'" name="delete_file" />';
echo '<input type="submit" class="delete-button" value="Delete image" />';
echo '</form>';
echo "</div>";
}
?>
You can try like this:
$fileName = basename($_POST['delete_file']);
// this will remove the path and leave only the name of the file.
$filePath = 'your full path'.'/'.$fileName;
// now build the 'mini image' full path - like '/var/www/project/img/travaux/villa/mini'.'/'.$fileName
if (file_exists($filePath)) {
unlink($filePath);
echo 'File '.$filePath.' has been deleted';
} else {
echo 'Could not delete '.$filePath.', file does not exist';
}
Note: this only deletes the 'mini' image. You'll need to add this to your existing code to also delete the main image
Just add this line of code after unlink main image:
unlink(pathinfo($filename, PATHINFO_DIRNAME).'/mini/'.pathinfo($filename, PATHINFO_BASENAME));
You should create a simple file remover function which takes an array of file names. And call the function with booth names. This is a flexible solution, because the changing parts takes place outside.
There is a vulnerability in your code, someone can delete any file from your website root folder, be careful!
You can try the following code:
$paths = array(
'orig' => '../img/travaux/villa/',
'mini' => '../img/travaux/villa/mini/',
);
if (array_key_exists('delete_file', $_POST)) {
$filename = basename($_POST['delete_file']);
foreach($paths as $path) {
if (file_exists($path.$filename)) {
if(!unlink($path.$filename)) {
$error = 'File '.$filename.' could not be deleted';
}
else {
echo 'File '.$filename.' has been deleted';
}
}
else {
$error = 'Could not delete '.$filename.', file does not exist';
}
if($error) echo $error;
}
}

PHP unlinking not working with variable

Going out of my mind with php unlinking
Here is my delete file script
$pictures = $_POST['data'];
//print_r ($pictures);
$imageone = $pictures[0];
$filename = "file:///Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/" . $imageone;
echo $filename;
if (is_file($filename)) {
chmod($filename, 0777);
if (unlink($filename)) {
echo 'File deleted';
} else {
echo 'Cannot remove that file';
}
} else {
echo 'File does not exist';
}
The above does not work, error response is file does not exist
however if i change the filename path to this (the echo data from the echo above)
$filename = "file:///Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/1420291529-whitetphoto.jpeg "
works fine and deletes the image.
Why can i not use the $imageone variable?
Do a print_r($pictures) to see if $pictures[0] is indeed the filename you're looking for.
Also note that if $pictures[0] is "//windows/*" you'll loose your windows if the user running PHP has administrative rights... so just using $pictures=$_POST["data"] is very VERY unsafe!

copy images from one folder to sub folder in php

I am facing a problem. I have some images stored in D://Images. I have to copy these images into D://Images/Modified. I tried but I did not get any output.
This is my code:
define("BASE_IMAGE_PATH","D:\\");
define("IMAGE_FOLDER_NAME","2014finalfour\\");
define("IMAGE_FOLDER_NAME_MODIFIED","modified");
define("IMAGE_File_Path",BASE_IMAGE_PATH.IMAGE_FOLDER_NAME);
define("IMAGE_File_Path_Modified", BASE_IMAGE_PATH.IMAGE_FOLDER_NAME.IMAGE_FOLDER_NAME_MODIFIED);
$srcdir=constant("IMAGE_File_Path");
$destdir=constant("IMAGE_File_Path_Modified");
echo $destdir;
if (!file_exists(IMAGE_File_Path_Modified))
{
mkdir(IMAGE_File_Path_Modified, 0777, true);
}
$srcdir=opendir($srcdir);
while($readFile = readdir($srcdir))
{
if($readFile != '.' && $readFile != '..')
{
if (!file_exists($readFile))
{
if(copy($srcdir . $readFile, $destdir . $readFile))
{
echo "Copy file";
}
else
{
echo "Canot Copy file";
}
}
}
}
closedir($srcdir);
Please help me to sought out it. It says it can not copy the file. copy() expects parameter 1 to be a valid path, resource given in C:\wamp\www\marcs\testmysql.php on line 3
You could use the copy() function :
copy('foo/test.php', 'bar/test.php');
example:
<?php
$file = 'images/folder/one.jpg';
$newfile = 'Images/folder/one_thumb.jpg';
if (!copy($file, $newfile)) {
echo "failed to copy";
}
Makes a copy of the file source to dest.
If the destination file already exists, it will be overwritten.

making a new folder using php code

I just wanna ask, what is the code needed in my code in order this warning will not show
Warning: mkdir() [function.mkdir]: File exists in
C:\xampp\htdocs\php-robert\dir\dir.php
also did my program is correct? what i want in my program is, if the folder is not exist, make a folder, if its existing just do nothing.. nothing to show, just nothing
dir.php
<?php
$var = "MyFolder";
$structure = "../../file/rep/$var";
if (!mkdir($structure, 0700)) {
}
else
{
echo"folder created";
}
?>
Try the following:
$folder = "folder_name";
// if folder does not exist or the name is used, just not for a folder
if (!file_exists($folder) || !is_dir($folder)) {
if (mkdir($folder, 0755)) {
echo 'Folder created';
} else {
echo 'Unable to create folder';
}
}
if (!is_dir($structure)) {
mkdir($structure);
}
else
{
echo "folder already exists";
}
if (is_dir($structure) == false and mkdir($structure, 0700) == false)
{
echo "error creating folder";
}
else
{
echo "folder exists or was created";
}
You could also test if a file exists, but it isn't a folder

Categories