I am uploading multiple images within a single category and I would like to store each group of images in a unique directory within my 'images/' directory as follows:
'images/unique_category/image1.jpg'
I have the following code but it is not creating a directory. I suspect it has something to do with setting the recursive parameter as 'true' which I believe I have done. Am I using the mkdir function incorrectly?
Thank you!
$unique_directory = "../images/".$_POST['item_name'];
if (is_dir($unique_directory)
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$unique_directory."/".$_FILES["file"]["name"]);
echo "Stored in: " . $unique_directory."/".$_FILES["file"]["name"];
}
else
{
mkdir($unique_directory, 0777, true);
move_uploaded_file($_FILES["file"]["tmp_name"],
$unique_directory."/".$_FILES["file"]["name"]);
Here, give this a try. I tested it on my (hosted) server and it works. Yet, I tested it by placing the files in the root of it, and used images instead of ../images for the $unique_directory variable.
I also used the chmod command apart from the other function, because the other method did not work.
N.B.: If possible, try changing 0777 to 0755, because using 0777 is not the safest setting.
<?php
$filename = $_POST['item_name'];
$unique_directory = "../images";
if (!is_dir($unique_directory . '/' . $filename)){
mkdir($unique_directory . "/" . $filename);
chmod("$unique_directory" . "/" .$filename, 0777);
}
if (is_dir($unique_directory))
{
move_uploaded_file($_FILES['file']['tmp_name'], $unique_directory . "/" . $filename . "/" . $_FILES['file']['name']);
echo "1) Stored in: " . $unique_directory . "/" . $filename . "/" . $_FILES['file']['name'];
}
else
{
move_uploaded_file($_FILES['file']['tmp_name'], $unique_directory . "/" . $filename . "/" . $_FILES['file']['name']);
echo "2) Stored in: " . $unique_directory . "/" . $filename . "/" . $_FILES['file']['name'];
}
?>
Related
So I want to be able to move the uploaded attachments to the specified directory depending on the month inside the year.
I'm getting the following two errors:
Warning: move_uploaded_file(): The second argument to copy() function cannot be a directory
Warning: move_uploaded_file(): Unable to move '/private/var/tmp/phptmfv0g' to 'uploads/2020/01'
Code:
$dir = "uploads/" . date('Y') . '/' . date('m');
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}
move_uploaded_file($_FILES['rtk']['tmp_name']['5']['file'], $dir);
So it creates the directory to build the folders, but I can't seem to move my contact form files into that directory at all - What could I be doing different?
This used to work by placing all the attachments in the same directory:
$file = "uploads/" . basename($this->get_last_name() . " - " . $this->get_first_name() . " - " . date("Y-m-d h:i:sa"));
move_uploaded_file($_FILES['rtk']['tmp_name']['5']['file'], $file);
You need to give the directory and filename for the destination:
$dir = "uploads/" . date('Y') . '/' . date('m');
$file = basename($_FILES['rtk']['name']['5']['file']);
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}
move_uploaded_file($_FILES['rtk']['tmp_name']['5']['file'], "$dir/$file");
Or if they exist, use the object methods as you did previously to build a filename:
$file = $this->get_last_name() . "-" . $this->get_first_name() . "-" . date("Y-m-d h:i:sa");
Exactly as the error says, the path you are trying to copy to is a directory not a file path
The problem is not about the month
It's that the path ends up as: uploads/2020/01
While it should be: uploads/2020/01/filename.ext
You can get original file name with $_FILES['file']['name'] --> $_FILES['rtk']['name']['5']['file']
$dir = "uploads/" . date('Y') . '/' . date('m');
$fileName = $_FILES['rtk']['name']['5']['file'];
$fullPath = $dir . '/' . $fileName;
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}
move_uploaded_file($_FILES['rtk']['tmp_name']['5']['file'], $fullPath);
--- To prevent users from replacing each other files you can add a unique prefix, for example just using uniqid()
$dir = "uploads/" . date('Y') . '/' . date('m');
$fileName = uniqid() . '_' . $_FILES['rtk']['name']['5']['file'];
$fullPath = $dir . '/' . $fileName;
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}
move_uploaded_file($_FILES['rtk']['tmp_name']['5']['file'], $fullPath);
Having some trouble with rewriting a photo file. I need the file name to get rewritten as a random string. The file uploads fine - I can't seem to get it copy the file and rewrite the file name to the random string. The file is going to stay in the directory.
The function is working fine and I can rewrite file name in the database, but it will not rewrite the actual file in the folder. The folder permissions are rwxr-xr-x (755).
Any thoughts?
function AfterUpdate(){
$file = $this->file_attachment;
$path_parts = pathinfo($file);
$newFilename = $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];
$file_src = $_SERVER['DOCUMENT_ROOT'] . $file;
$newfile_src = $_SERVER['DOCUMENT_ROOT'] . $newFilename;
if (move_uploaded_file($file_src, $newfile_src)){
$this->file_attachment = $newFilename;
}
}
$newFilename contains a path location I guess by looking at the $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];.
$newFilename should just be the new file name with extension.
move_uploaded_file will only move files from one folder to another or the same, that already exists. But will not create a folder for you.
Simple fix. Replace move_uploaded_file with rename. The file will not be moved, just renamed.
$file = $this->file_attachment;
$path_parts = pathinfo($file);
$newFilename = $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];
$file_src = $_SERVER['DOCUMENT_ROOT'] . "/" . $file;
$newfile_src = $_SERVER['DOCUMENT_ROOT'] . "/" . $newFilename;
if (rename($file_src, $newfile_src)){
$this->file_attachment = $newFilename;
}
Why does the rename function not work?
$new_image_name = $maximum_id + 1 . '.jpg';
$old_image_name = $file_name;
// Rename the image file
rename($profile_folder . $old_image_name, $profile_folder . $new_image_name);
var_dump($profile_folder . $old_image_name, $profile_folder . $new_image_name);
gives me string(56) "/var/www/myproject/public/images/1/profile/old.jpg" string(50) "/var/www/myproject/public/images/1/profile/1.jpg"
var_dump(rename($profile_folder . $old_image_name, $profile_folder . $new_image_name));
returns (bool)false
The paths are correct, however the file is not renamed and uploaded with the old name. What am I doing wrong?
Here's what I currently have:
$pic_path provides a path like so: path/to/img
$toid is a number, and like pic_path, no slash before or after.
$file_path = glob( $pic_path . "/" . $toid . ".*" );
array_map('unlink', $file_path);
The picture I am trying to delete is indeed in the specified path.
I've tried using:
$file_path = glob( $_SERVER['DOCUMENT_ROOT'] . "/" . $pic_path . "/" . $toid . ".*" );
array_map('unlink', $file_path);
Also tried:
$file_path = glob($pic_path . "/" . $toid . ".*");
array_walk($file_path, function ($file) {
unlink($file);
});
and I've also tried replacing the * wild card for the actual image extension.
Any idea?
But what's the best and easiest way to copy a file or folder between a local and remote server using php? These are files located above the web folder, so I'll need to use paths instead of the URL.
I would do it using PHP's built-in FTP functions.
EDIT: Ahh, you want secure. This is what I would use then: SSH2-SFTP
Well i made this function hope it works for you copy files from ftp:
$ftpConnection = the conection, example ftp_connect(1.0.0.1).
$path = the ftp path.
$destination = the local file.
function ftpRecursiveFileListing($ftpConnection, $path, $destination) {
$contents = ftp_nlist($ftpConnection, $path);
foreach ($contents as $currentFile) {
if (strpos($currentFile, '.') === false) {
$dir = basename($currentFile);
echo "<br> <b> Directorio </b>" . $dir;
mkdir($destination . "/" . $dir);
ftpRecursiveFileListing($ftpConnection, $currentFile, $destination . "/" . $dir);
} else {
$file = basename($currentFile);
echo '<br> <b>archivo </b>' . $file;
echo '<br> <b>path </b>' . $path;
echo '<br> <b>completo </b>' . $path . "/" . $file;
ftp_get($ftpConnection, $destination . '/' . $file, $path . '/' . $file, FTP_BINARY);
}
}
}