Upload the same file into multiple folders - php

I have an uploading form, and when i select the file,i need it to be uploaded in multiple folders.Any ideas how to do that? I've tried with a loop like the following one:
foreach($_POST['check'] as $check){
move_uploaded_file($_FILES['doc']['tmp_name'], $target_path);
chmod($target_path,0777);
}
But it only upload once.Any ideas please?

After uploading, copy the file from the target path to the other paths with copy().
foreach($_POST['check'] as $check){
move_uploaded_file($_FILES['doc']['tmp_name'], $target_path);
chmod($target_path,0777);
// and now...
copy($target_path, $target_path_2);
copy($target_path, $target_path_3);
// etc...
}
By the way, setting 0777 for permissions generally is unnecessary and a bad idea. You want anyone to upload files and let any user execute them? That's the way to start giving anyone full control over your machine.
Also, are you sure that you need the file on multiple places? Why not have one common storage folder and create symbolic links to it? But that depends on your setup, of course.

Upload once and then just copy() it.

$i = 0;
foreach($_POST['check'] as $check){
$basePath = "/var/www/html/more/phpexm/";
$target_path = $basePath . $check;
if (!file_exists($target_path)){
mkdir($target_path, 0777);
}
if ($i == 0){
$sFileNameTmp = $_FILES['doc']['tmp_name'];
$sFileName = $_FILES['doc']['name'];
move_uploaded_file($sFileNameTmp, $target_path . '/' . $sFileName);
$sFirstFileUploaded = $target_path;
}
else{
copy ($sFirstFileUploaded . '/' . $sFileName, $target_path . '/' . $sFileName);
}
$i++;
}

Related

php file uploading cannot retrieve the path

I am trying to upload photos in to folder uploads and its path to be recorded under photo in DB. This is my code:
$folder ="uploads";
$destFile = $folder . basename($_FILES["photo"]["name"]);
$sourdeFile = $_FILES["photo"]["tmp_name"];
if(move_uploaded_file($sourdeFile,$destFile)){
echo "File has been uploaded";
$photo = $destFile;
}else{
echo $_FILES['photo']['error'];
$photo = "images/default.png";
}
When I upload photos they successfully uploaded into folder but the problem is its path recorded as follow :
uploads42141402_1866830986743601_8538143552767524864_n.jpg
But to view photos in a page there should be \ next to uploads. So I tried to change my code as follow.
$folder = "uploads\";
But it generates this error
Can anyone say how to fix this ?
How about
$folder ="uploads/";
I've tried and it works. Maybe $folder ="uploads" . DIRECTORY_SEPARATOR; will better than "/".
For the error its looks uploads folder is not there or it not have permission
create a upload folder or try with absolute path
you can make use of dirname(__FILE__) or $_SERVER['DOCUMENT_ROOT'] for creating dynamic path

PHP move_uploaded_file won't work - is_writable and is_dir = true

I am having trouble uploading file on server.
My code:
$path = "produkty/$kategorie/$last_id/";
if(!is_dir($path)){
mkdir($path, 0777, true);
}
if (is_dir($path) && is_writable($path)) { echo "ok"; }
And it returned ok.
The upload code looks like this:
move_uploaded_file($_FILES["uvodniobr"]["tmp_name"], $path."uvodniobr.".$ext)
If I echo out:
echo $path."uvodniobr.".$ext;
It will return this: produkty/kategorie/35/uvodniobr.png
Things like checking file size, extension works fine, it just won't upload.
Do you know what could be wrong with my code? Thank you
SOLVED
EDIT: I was trying to upload file over 2MB without changing php.ini in xampp/php/php.ini upload_max_filesize=2M.
Thank you for your answers.
I guss problem is because you're using relative path in move_uploaded_file while it needs absolute path So
You should use document_root to get absolute path like this:
$target_path = $_SERVER['DOCUMENT_ROOT'] . '/' .$path; // if not work try $target_path = $_SERVER['DOCUMENT_ROOT'] . $path;
move_uploaded_file($_FILES["uvodniobr"]["tmp_name"],
$target_path."uvodniobr.".$ext);
Also it's better to surround it with if to check the result
$target_path = $_SERVER['DOCUMENT_ROOT'] . '/' .$path; // if not work try $target_path = $_SERVER['DOCUMENT_ROOT'] . $path;
if (move_uploaded_file($_FILES["uvodniobr"]["tmp_name"],
$target_path."uvodniobr.".$ext)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
Helper link move_uploaded_file & relative path
It was my fault, I forgot to change upload_max_filesize=2M to upload_max_filesize=16M in xampp/php/php.ini
My bad, sorry and thank you for your effort I really appreciate it.

How to copy an image from one folder to other in 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);
}

PHP copy all images folder from one folder to another

I am working on some code that will take all the images in one folder and copy them into another folder and then delete the original folder and it content.
I have:
copy('images/old-folder/*', 'images/new-folder/');
unlink('images/old-folder/');
but this does not work :( but it doesn't work I mean the files dont copy over and the old-folder is not deleted :(
I even tried:
system('cp images/old-folder/* images/new-folder/');
and that didn't work either :( Please help.
I have even tried to change the permissions of the two folders:
chmod('images/old-folder/', 0777);
chmod('images/new-folder/', 0777);
foreach(glob('images/old-folder/*') as $image) {
copy($image, 'images/new-folder/' . basename($image)); unlink($image);
}
rmdir('images/old-folder');
check the docs: glob, rmdir, also you might find user comments on rmdir useful.
EDIT:
added basepath to the second parameter to the copy function which has to be an actual path and not a directory.
<?php
$src = 'pictures';
$dst = 'dest';
$files = glob("pictures/*.*");
foreach($files as $file){
$file_to_go = str_replace($src,$dst,$file);
copy($file, $file_to_go);
}
?>
Found here: PHP copy all files in a directory to another?
Here is a modified version from #Prasanth that should work (not tested)
<?php
$oldfolder = 'images/new-folder';
$newfolder = 'images/old-folder';
$files = glob($oldfolder . '/*');
foreach($files as $file){
$filename = basename($file);
copy($file, $oldfolder . '/' . $filename);
unlink($file);
}
rmdir($oldfolder);
?>

Problem to upload audio file in php

Here is my code:
if ($_FILES['music']['name'] != '')
{
$file_name = time() . $_FILES['music']['name'];
copy($_FILES['music']['tmp_name'], "music/" . $file_name);
}
else
{
$file_name = "";
}
I want to upload audio file. the file name is insert into database. but its not insert into folder.
try move_uploaded_file instead. You may want to check file size limits too.
I think you want to use move_uploaded_file(), not copy
I guess the folder in which you are trying to copy the uploaded file is not the one you expect. Possibly this is what you need:
copy($_FILES['music']['tmp_name'], __DIR__ . "/music/" . $file_name);
By the way move_uploaded_file() works faster and safer than copy().

Categories