I want to create a directory based on the username(user) that is logged in, to upload images to that folder, so when each user uploads an image file it will be sent to the directory based on the username and the image file is saved in that folder
$_SESSION["user"];
I use session to get the username, now I want to create a folder inside a folder name uploads
mkdir('/uploads/' . $_SESSION['user']);
and make $target_dir= location for upload as above one.
Use of mkdir() and its parameter:
recursive Allows the creation of nested directories specified in the pathname. Defaults to FALSE.It means
Path : path of directory including directory name.
Permission : 0777
Recursive Flag : when you need to create subfolder.
$path = '/upload/UserA';
mkdir($path, 0777, true);
Note
/upload/ is almost certainly wrong. It points to the "upload" directory in the root directory, where you most likely don't have the permission to create one.
Related
I have absolutely no idea where to start with this idea.
What I want to do is:
Have a directory: folder/
Then when any folder is created within that folder, a link is automatically created, linking to the newly created folder and have the link have the name of the folder.
Then after all that, I want to have a file created, named (thefoldername.php) at the root of my server.
Any ideas? Thanks.
I think this is what you are asking:
$path='folder' ;
foreach (glob($path . "/*", GLOB_ONLYDIR) as $dir){
echo ''.$dir.'';
}
a couple of assumptions:
1) your server has write permissions to the webroot.
2) you've captured the name of the folder are creating (e.g. tmp)
3) you want a link added to some file in webroot.
you can then use mkdir to make the directory. say in $path.'/'.$tmp where $tmp is name of newly created folder and $path is folder/
then create a file like with file_put_contents (contents of file ??? perhaps including a link to the new file.) this part is unclear.
I am creating a medium size application.
This application consists of a lot of products.
Now these products have many images (one product can have 5 - 6 images)
To try and make some sort of ordering I want to create one folder for each product this folder contains all images that is bound to the product.
Now so far I have tried the following:
move_uploaded_file($file, APP.'product_images/'.$product_id.'/'.$image['name']);
However when I try this I get the following error:
Warning (2): move_uploaded_file(/var/www/udlejnings-priser/cake/app/product_images/22/afterClick.png): failed to open stream: No such file or directory [APP/Controller/ImagesController.php, line 56]
Warning (2): move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php472ci6' to '/var/www/udlejnings-priser/cake/app/product_images/22/afterClick.png' [APP/Controller/ImagesController.php, line 56]
Now I am not a complete noob and know that this means that I am missing permissions to the folder.
However the problem is that if the folder does not exist (i.e this is the first time an image for that product is uploaded) then a new folder should be created.
My question is two parted.
Does this automatically create a new folder if it doesn't already exist?
How can I give permission to a newly created folder so that I avoid this problem?
[I] know that this means that i am missing permission to the folder.
Actually no =). The error message reads:
failed to open stream: No such file or directory
Which makes no reference to permissions the problrm is: the containing-folder you're trying to write to doesn't exist.
Does this automatically create a new folder if it doesn't already exist?
No.
How can i give permission to a newly created folder?
It's not necessary to do so - anything created will have the correct permissions to permit the webserver user to read the files. However first it's necessary to try and create a folder, which in the question isn't the case.
Using CakePHP, the Folder class can be used to do that:
App::uses('Folder', 'Utility');
$dir = new Folder('/path/to/folder', 2);
The second parameter is used to create a new folder if it doesn't exist. In the context of the question that means the code would look something like this:
function whatever() {
if ($this->request->data) {
...
$unused = new Folder(APP.'product_images/'.$product_id, true);
if (move_uploaded_file($file, APP.'product_images/'.$product_id.'/'.$image['name'])) {
...
} else {
...
}
}
}
The folder APP/product_images should already exist, and must have permissions such that the webserver user (e.g. apache) can write to it otherwise it will not be possible to create the sub-folders/upload files. Assuming APP/product_images exists and the webserver user has permissions to write to it, there is no need to modify permissions of uploaded files - files created by a user are by default readable by that user.
Try this:
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
1) Does this automaticly create a new folder if it doesnt already exist. => file_exists and mkdir
2) how can i give permission to a newly created folder so that i avoid this problem => 0777
No, it won't create the folder dynamically.
Use chmod() to change permissions.
For checking the existence of some folder you can use is_dir() too.
It's always a good idea too add the magic constant
__DIR__
to the file or directory path.
( __DIR__
gives out the path to the directory in which your script is located). In the errormessages "APP" is highlighted in a different colour than the path name. This could be a hint that the path cannot be located.
I'm making a content management system for a website I built. I want the system to be discrete, so I made it exist in only one PHP file, called '_admin.php'. All the content displayed in this file comes from includes that I store in a sub-folder called 'admin' (out of the way).
The photos used on the website are stored in an 'assets' folder that also sits in the root dir. The admin page has direct access to the assets folder, as it is also in the root. But the upload file script sits a few directories into the 'admin' folder and I want the uploaded files to be stored in the assets folder.
The move_uploaded_file() method takes the destination path for the file, but it requires a direct path. I try using $_SERVER['DOCUMENT_ROOT'], but the resulting directory doesn't seem to have any of my files. If I use getcwd() in a doc in the root, it returns the actual file structure that I can use. The same if I echo out __FILE__. But I've experimented with this SERVER constant a lot and I can't locate my website with it.
Since the script that uploads the images is called as a form action, I can't pass the root directory as a variable.
Not really sure what I'm doing wrong, anyone have any ideas?
Thanks
edit **
//See if Files array contains new files
if (!empty($_FILES['file'])){
foreach($_FILES['file']['name'] as $key => $name){
$error = $_FILES['file']['error'][$key];
$temp_name = $_FILES['file']['tmp_name'][$key];
$dir = getcwd();
$move_file = move_uploaded_file($temp_name, "$dir/temp/$name");
if (($error == 0) && ($move_file)){
$uploaded[] = $name;
}else{
die($error);
}
}
echo __FILE__;
echo "<br/>";
echo __DIR__;
echo "<br/>";
echo $_SERVER['DOCUMENT_ROOT'];
exit();
}
The upload script I'm currently using. This script works fine because I'm storing the images in the same directory as the script. I just don't know how to store them in my root.
I would specify the full absolute file path if you can. I set this via define() in a config file for my CMS. On install you figure out what that path is and set it.
define("BASEFILEPATH", "/home/.../[webroot]"); // The base file path for the website
You may be looking for something more general, but you could have some sort of install script where the user can enter basic info into a form, such as username, pwd, etc. and you could have them enter this path as well.
Let's say, in PHP, I am trying to put an image in a specific directory that is on root.
I want to put it on /images/afc/esp/stadium/ directory. Images folder, Federation folder, Country ISO3 folder, content folder.
$folder_full = "images/".$getFed."/".$country_folder."/stadiums";
if (!is_dir($folder_full)) mkdir($folder_full);
Before you ask, yes $getFed and $country_folder work and output text. So, then I get this error: Warning: mkdir(): No such file or directory
I don't get it?
Some of your subdirectories don't exist so you either need to iteratively create them or set the 3rd argument to mkdir() to true. Note that the second argument are the directory permissions (ignored on Windows) which default to 0777.
You also need to set $folder_full to the root using /.
$folder_full = "/images/{$getFed}/{$country_folder}/stadiums";
if (!is_dir($folder_full)) mkdir($folder_full, 0777, true);
You need to use the recursive parameter to add directories that don't exist in the path you provide: mkdir($folder_full, 0777, true)
See the PHP docs here
All the intermediary directories have to already exist. You can trigger this behaviour by using the optional third argument:
mkdir($folder_full,0777,true);
in normal cPanel the folder permissions should be 0755
so the command in this case will be:
mkdir('tst/tst2/tst3', 0755, true);
I have this directory structure:
\htdocs
\htdocs\uploadfiles\
Now if a user's id is s001, then I should create a new folder for this user to store uploaded files.
\htdocs
\htdocs\uploadfiles\
\htdocs\uploadfiles\s001\
Here are my questions:
Q1> What mode should I use for \htdocs\uploadfiles
Q2> What mode should I use for \htdocs\uplaodfiles\s001\
Based on http://www.elated.com/articles/understanding-permissions/
The function I use to create the folder for the user is as follows:
mkdir("/htdocs/uploadfiles/s001/", 0700);
That means only the owner of the s001 has access read/write/execute. But I don't know whether or not this is correct and practical.
That is correct. Make the owner of the directory the same user as the web server. Add ACLs to all of the directories if you need other users to have access.
The will be 777 to /uploadfiles and rest 755 while creating folder
mkdir("/htdocs/uploadfiles/s001/", 0755);