I am trying to create a random directory for saving uploaded photos using the below code but its not working. Can anyone help.
//Photo upload script
if(isset($_FILES['profilepic']))
{
if(((#$_FILES["profilepic"]["type"]=="image/jpeg")||(#$_FILES["profilepic"]["type"]=="image/png")||(#$_FILES["profilepic"]["type"]=="image/gif")) && (#$_FILES["profilepic"]["size"]<2048576))
{
$chars = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM123456789";
$rand_dir_name=substr(str_shuffle($chars),0,15);
mkdir("./userdata/images/$rand_dir_name");
//mkdir("\\userdata\\images\\".$rand_dir_name,077,true); //tried this but no luck
if(file_exists("userdata/images/$rand_dir_name/".#$_FILES["profilepic"]["name"]))
{
echo #$_FILES["profilepic"]["name"]."Already exists";
}
else
{
move_uploaded_file(#$_FILES["profilepic"]["temp_name"],"userdata/images/$rand_dir_name/".$_FILES[profilepic][name]);
echo "Uploaded and Stored in userdata/images/$rand_dir_name/".#$_FILES["profilepic"]["name"];
}
}
else
{
echo "error";
}
}
mkdir("./userdata/images/$rand_dir_name");
This line should be either:
mkdir("../userdata/images/$rand_dir_name"); //in this format if you are using one directory back to the relative path
or
mkdir("/userdata/images/$rand_dir_name"); //in this format if you are trying to create from projects root path
or
mkdir("userdata/images/$rand_dir_name"); //in this format if you are trying to create a directory from the relative path
it cannot have single dot (.) followed by forward slash
Related
I have used the following code to unzip a zip folder after upload.
/* here it is really happening */
if(move_uploaded_file($source, $targetzip)) {
$zip = new ZipArchive();
$x = $zip->open($targetzip); // open the zip file to extract
if ($x === true) {
echo $targetdir; exit;
$zip->extractTo($targetdir); // place in the directory with same name
$zip->close();
unlink($targetzip);
}
$message = "Your .zip file was uploaded and unpacked.";
} else {
$message = "There was a problem with the upload. Please try again.";
}
It is working fine. But the only problem is the that the folder is replaced by the new one uploaded. I want to merge it.
Suppose i have a zip folder.
One.zip
a
b
c
When i upload this, it should merge with the (One) folder which is already in the upload server. So the previous folders in the (One) folder should not be replaced.
I think It is understandable now. Please help to do the trick.
You can con-cat current time & date with you target fine name:
$targetdir = $targetdir."_".str_replace(' ',"/",date('Y-m-d H:i:s'));
Using this logic, you fine will not replace with previous file.
I have two directories:
/publichtml
/csp.example.com
My code is in csp.example.com/classes/ep-class.php and I want to upload my file on /publichtml/upload/photo folder. How can I achieve that also /publichtml is accessible using example.com so I am taking target directory as a absolute url?
Here is my code:
if(isset($_FILES["imagefile"]["type"][0]))
{
$filename=$_FILES["imagefile"]["name"][0];
// Storing source path of the file in a variable
$sourcePath = $_FILES['imagefile']['tmp_name'][0];
// Target path where file is to be stored
$targetPath = SITEURL."upload/photo/".$filename;
if(move_uploaded_file($sourcePath,$targetPath))
{
return "Yes";
}
else
{
echo $_FILES["imagefile"]["errors"][0];
die("File upload error !");
}
}
Any help would be highly appreciated.
I am trying to delete a file from folder in php here is my model function
function deleteFiles()
{
$file = "http://localhost/copycopy/img/uploaded/long.jpeg";
if(is_file($file))
{
#unlink($file); // delete file
echo $file.'file deleted';
}
else
{
echo "no file";
}
}
but I always see "no file" and file is never deleted, file is in folder,because the url in $file actually displays the file in browser
help me
instead of using web url
$file = "http://localhost/copycopy/img/uploaded/long.jpeg";
use local path to file:
$file = $pathToYourWebSite . "/copycopy/img/uploaded/long.jpeg";
be sure to set $pathToYourWebSite to real location of your website;
<?php
include('includes/db.php');
$drinks_cat = $_POST['drinks_cat'];
$drinks_name = $_POST['drinks_name'];
$drinks_shot = $_POST['drinks_shot'];
$drinks_bottle = $_POST['drinks_bottle'];
$drinks_availability = 'AVAILABLE';
$msg = "ERROR: ";
$itemimageload="true";
$itemimage_size=$_FILES['image']['size'];
$iname = $_FILES['image']['name'];
if ($_FILES['image']['size']>250000){$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>";
$itemimageload="false";}
if (!($_FILES['image']['type'] =="image/jpeg" OR $_FILES['image']['type'] =="image/gif" OR $_FILES['image']['type'] =="image/png"))
{$msg=$msg."Your uploaded file must be of JPG , PNG or GIF. Other file types are not allowed<BR>";
$itemimageload="false";}
$file_name=$_FILES['image']['name'];
$add="images"; // the path with the file name where the file will be stored
if($itemimageload=="true")
{
if (file_exists($add) && is_writable($add))
{
if(move_uploaded_file ($_FILES['image']['tmp_name'], $add."/".$_FILES['image']['name']))
{
echo "Image successfully updated!";
}
else
{
echo "Failed to upload file Contact Site admin to fix the problem";
}
}
else
{
echo 'Upload directory is not writable, or does not exist.';
}
}
else
{
echo $msg;
}
$dir = $add."/".$iname;
echo "<BR>";
// Connects to your Database
mysql_query("INSERT INTO `product_drinks`(`drinks_id`, `drinks_cat`, `drinks_name`, `drinks_shot`, `drinks_bottle`, `drinks_image`, `drinks_availability`) VALUES (NULL,'".$drinks_cat."', '".$drinks_name."','".$drinks_shot."','".$drinks_bottle."','".$dir."','".$drinks_availability."')") or die("insert error");
Print "Your table has been populated";
?>
The code I'm working on works but i have to create a new "image" folder for my admin folder. Is there any way that I could upload the file outside the admin folder and move it to to the original "image" folder". I know it's quite confusing but my directory looks like this.
clubmaru
-admin
-images
-css
-images
-js
You may be looking for PHP's rename function. http://php.net/manual/en/function.rename.php
Set the oldname parameter to the file (with its path) and the newname parameter to where you want it to be (along with the new path, obviously)
Just ensure the "image folder" you want to move the file to has the correct permissions set ensure it's writable. You also may want to consider changing the parameter in your move_uploaded_file to put the file where you want it in the first place!
Yes there is a way, you need to change the path. Right now you have the path as images/$name which means that it will put the file in the images directory found in the local directory to the script that is running.
Using directory layout:
clubmaru
->admin
->script.php (the upload file)
->images
->css
->images
->js
You make the path relative (or find another alternative)
$add="../css/images";
This means, go up a directory, go into css then into images.
I have a filename stored with the directory as a value.
Ex. /var/www/remove_this.php
In my PHP script I want to remove everthing after the last '/', so I can use mkdir on this path without creating a directory from the filename also.
There are so many string editing functions, I don't know a good approach. Thanks!
dirname() will return you the directory part of the path
Use pathinfo() to get info about the file itself.
$file = '/var/www/remove_this.php';
$pathinfo = pathinfo($file);
$dir = $pathinfo['dirname']; // '/var/www/'
You could use string functions, but for this case PHP has some smarter directory functions:
$dir = dirname('/var/www/remove_this.php'); // /var/www
pathinfo is an excellent one as well.
<?php
$file="/var/www/remove_this.php";
$folder=dirname($var);
if (!file_exsts($folder))
{
if (mkdir($folder,777,true))
{
echo "Folder created\n";
} else
{
echo "Folder creation failed\n";
}
} else
{
echo "Folder exists already\n";
}
?>