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
Related
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';
I tried deleting file if its already existing .
But i ended up with no result.
Can any one help me with this!!!
$path_user = '/wp-content/plugins/est_collaboration/Files/'.$send_id.'/';
if (!file_exists($path_user)) {
if (mkdir( $path_user,0777,false )) {
//
}
}
unlink($path_user);
if(move_uploaded_file($file['tmp_name'],$path_user.$path)){
echo "Your File Successfully Uploaded" . "<br>";
}
Organize your code, try this:
$path = 'filename.ext'; // added reference to filename
$path_user = '/wp-content/plugins/est_collaboration/Files/'.$send_id.'/';
// Create the user folder if missing
if (!file_exists($path_user)) {
mkdir( $path_user,0777,false );
}
// If the user file in existing directory already exist, delete it
else if (file_exists($path_user.$path)) {
unlink($path_user.$path);
}
// Create the new file
if(move_uploaded_file($file['tmp_name'],$path_user.$path)) {
echo"Your File Successfully Uploaded"."<br>";
}
Keep in mind that PHP will not recursively delete the directory contents, you should use a function like this one
Maybe you missing else condition ?? And file_name variable :
$file_name = 'sample.jpg';
$path_user = '/wp-content/plugins/est_collaboration/Files/'.$send_id.'/';
if (!file_exists($path_user.$file_name))
{
if (mkdir( $path_user,0777,false )) {
}
} else {
unlink($path_user.$file_name);
}
I am trying to create a directory after a form submit, then after the form is submitted I want that directory to have a file stuffed inside of it. I have tried the php copy function and used an if statement to see if it was copying successfully but it is not. Please take a look at my code and see what is going on. And when it was working, it would just give a "1" output once submitted. No actual file was moved inside of the folder.
if($_POST['submit']=='Register')
{
// If the Register form has been submitted
$root = "/serves/registered.php";
$err = array();
$folder = mkdir($_POST['username']);
$reg = "registered.php";
mkdir($_POST['username']);
copy($root,$folder);
if (!copy($root, $folder)) {
echo "failed to copy $root...\n";
} else {
echo "Account was successfuly created.";
}
Thank you
This can do it:
$root = "serves/registered.php";
$folder = mkdir($_POST['username']);
if($folder) {
$reg = "registered.php";
if (!copy($root, $_POST['username']."/".$reg)) {
echo "failed to copy $root...\n";
} else {
echo "Account was successfuly created.";
}
}
else {
echo "Could not create folder";
}
if($_POST['submit']=='Register')
{
// If the Register form has been submitted
$root = "contact.php";
$folder = $_POST['username'] . '/' . $root;
mkdir($_POST['username']);
if (!copy($root, $folder)) {
echo "failed to copy $root...\n";
} else {
echo "Account was successfuly created.";
}
}
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);
I have a PHP file which creates a unique directory for each user when a file is uploaded. I would like the script to check and see if the directory already exists and if so then skip the mkdir action. Here is my code sample:
<?php
$thisdir = getcwd();
$new_dir = "123";
$full_dir = $thisdir . "/upload/" . $new_dir;
if(mkdir($full_dir, 0777))
{
echo "Directory has been created successfully... <br>";
}
else
{
echo "Failed to create directory...";
}
?>
To continue this example, please assume that the folder "123" already exists. How do I modify it for this case? I am thinking it must be some sort of if...else statement. Thanks for going through this issue.
Use is_dir() to find out whether the folder already exists.
function maybe_mkdir($path, $mode) {
if(is_dir($path)) {
return TRUE;
} else {
return mkdir($path, $mode);
}
}