I have created this admin user page where data can be inserted. But everytime I try to execute it says:
mkdir(): File exists in rental/search/add_property_2.php on line 98
Here is the code:
$p_img9 = $_POST['p_img9'];
$p_img10 = $_POST['p_img10'];*/
if (!file_exists('tmp_imgs/'.$property_id)) { // Creating Temprory Directroy for images
mkdir('tmp_imgs/tmp_'.$property_id, 0777, true); ...Line 98
}
foreach ($_FILES['p_img']['name'] as $f => $name) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $name);
$extension = end($temp);
Seeking Help On This
Property Id is auto generated and on previous page to this its defined as
$property_id = get_new_property_id();...
and second page where getting error
$property_id = $_POST['property_id'];..
Your check condition doesn't match the directory you are creating. You can fix it like so
$directory = 'tmp_imgs/tmp_'.$property_id;
if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
Related
I'm trying to upload a photo to mysql database using php, however I'm getting the following error:
failed to open stream: Permission denied in '/The location of my php file' on line 40.
move_uploaded_file(): Unable to move '/Applications/XAMPP/xamppfiles/temp/phpgZi5gV to 'The location where the image will be stored'
Here is my code:
$dir = $path;
$default = "default.png";
// get Filename
$filename = basename($filen['name']);
$targetFilePath = $dir.$filename;
$filetype = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if(!empty($filename)) {
//allowed file formats
$allowedFormats = array("jpg", "png", "jpeg", "gif", "pdf");
if(in_array($filetype, $allowedFormats)) {
if(move_uploaded_file($filen['tmp_name'], $targetFilePath)) {
return $targetFilePath;
}
}
Looks like you're dealing with some permissions issue where your application server doesn't have permission to move the images from their current folder /Applications/XAMPP/xamppfiles/temp/.
Can you change the permissions of the folder to see if that fixes the issue ?
Run the command bellow:
chmod 777 /Applications/XAMPP/xamppfiles/temp/
I want to check if an extension is part of an array:
So: if an extension is not part of a forbidden array; do something is allowed
$ext = $path_info['extension'];
$ForbiddenExts = array("php", "html", "htm");
if( $ext != in_array($ForbiddenExts)){
// do something allowed
Change your code to:
$ext = $path_info['extension'];
$ForbiddenExts = array("php", "html", "htm");
if(!in_array($ext, $ForbiddenExts))
{
// do something
}
Check this link for more explanation.
Other than using
if( $ext != in_array($ForbiddenExts)){
You can use
if(!in_array($ext, $ForbiddenExts)){
//your code
}
You have to practice checking the mime type too. Otherwise it may cause errors.
for eg: if someone edit the extension of a ".txt" file to ".pdf"
(assuning pdf is allowed type). Then if you don't check mime type, the
code will accept the file as pdf
Change your code to:
$fileName = 'banner.jpg';
$fileNameParts = explode('.', $fileName);
echo $ext = end($fileNameParts);
$allowed_extensions = array("jpg", "jpeg", "png");
if(in_array($ext, $allowed_extensions))
{
echo 'Allowed Extension';
// do something
}else{
echo 'Not Allowed Extension';
// do something
}
Check this link for more explanation.
Been looking around as i'm having a few issues with PHP file upload! I'm trying to upload a photo or three to a database (admin_images) if the user wants to or show the photo that's already stored in the db. I'm having some issues, below is my code I currently have, any advice is welcome.
require_once 'connect/config.php';
require_once 'connect/opendb.php';
require_once 'connect/magic.quotes.php';
$home_query = "SELECT * FROM admin";
$home_rt = mysql_query($home_query);
$home_row = mysql_fetch_assoc($home_rt);
if(isset($_POST['submit'])) { $HomeTitle = check_input($_POST['HomeTitle']); $HomeBio = check_input($_POST['HomeBio']);
$home_query = mysql_query("UPDATE admin SET HomeTitle = '".$HomeTitle."', HomeBio = '".$HomeBio."' WHERE ID = 1");
foreach ($_FILES['file']['name'] as $f => $name) {
$name = strtolower($name);
$allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $name); $extension = end($temp);
if(in_array($extension, $allowedExts)) { if($_FILES['file']['size'][$f] < 2000000) {
$uniqid = uniqid();
move_uploaded_file($_FILES["file"]["tmp_name"][$f], "upload/" . $uniqid . "." . $extension);
mysql_query("INSERT INTO admin_images (id, HomeImage1, HomeImage2, HomeImage3) VALUES (".$last_id.", '".$uniqid .".".$extension."')");
} else {
} } else {
} } header("Location: home1.php");
} require_once 'connect/closedb.php'; ?
In the PHP further down the page I have included:
input type="file" name="files[]" multiple
Apologies if I'm being unclear.
Thanks in advance.
By using $_FILES['file'] you are telling the PHP to get file from input type with name "file" -the input you show has a different name (actually its an array).
it is best to dump the $_FILES array to view the actual data in it.
var_dump($_FILES);
see this. it gives detail process. match the format and check for any bug.
I searched online for a code that does PHP image upload using ajax. I found the code attached below. The issue is that I changed few things (minor tweaks) to make it work on my server. Originally it was just a php page (not a class or function) that handles the data posted from form. I made it into class then function. I am following OOP now. I thought the best way to do things in the conversion from procedural to OOP was to pass $_FILES and $_POST to a method and inside deal with them. I think this didn't work. Look at the example and please advise on how to go forward.
function uploadImageChosen($_FILES, $_POST){
$path = "../uploads/images/";
$valid_formats = array("jpg", "png", "gif", "bmp");
$connectionInstance = new ConnectionClass();
$connectionInstance->connectToDatabase();
$imgName;
$imgURL;
$imgSize;
$imgDir = $_POST['directory'];
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$imgSize = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$imgName = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$imgName))
{
$imgURL = $path.$imgName;
$connectionInstance->query("INSERT INTO imagesupload(id, title, url, size, directory) VALUES (null, '$imgName','$imgURL', '$imgSize', '$imgDir')");
//echo "<img src='uploads/".$imgName."' class='preview'>";
}
else{
echo "failed";
}
}else{
echo "Image file size max 1 MB";
}
}else{
echo "Invalid file format..";
}
}else{
echo "Please select image..!";
}
}//end of if
}//end of function
As to the page where the class function is being called, here it is:
<?php
require_once("../classes/UploadImages.php");
$uploadInstance = new UploadImages();
$uploadInstance->uploadImageChosen($_FILES, $_POST);
//header("LOCATION:portfolio.php");
?>
Thank you very much :)
$_POST and $_FILES are superglobal arrays, they are always available, and redefining them in a function or method is a bad idea.
You can do something like this:
$uploadInstance->uploadImageChosen();
..
function uploadImageChosen(){
$path = "../uploads/images/";
$valid_formats = array("jpg", "png", "gif", "bmp");
...
$name = $_FILES['photoimg']['name'];
...
Or if you need copies in the local scope do it like this:
$uploadInstance->uploadImageChosen($_FILES, $_POST);
..
function uploadImageChosen($files, $post){
$path = "../uploads/images/";
$valid_formats = array("jpg", "png", "gif", "bmp");
...
$name = $files['photoimg']['name'];
...
Try removing $_SERVER['REQUEST_METHOD'] == "POST" from the IF-statement, and see what it does.
The script above has a problem which was not detected in error log. The problem was first presented when I instantiated the connection class. I should have created another variable that would receive the open connection for querying to work That was solved and data now present in the DB. The second problem was the JPG format was not in array of acceptable types, so I added that along with jpg (variations). This made the file actually transfer to upload folder. Thank you all for support and sorry for the inconvenience :)
Here I have the problem with uploading image in PHP.
The problem is that when first time I upload the image file it works fine.
But when I am trying to upload the file second time without page refresh it takes first image name and upload it.
What is the problem and how can it be resolved?
$name = $_FILES['ImageFile']['name'];
$size = $_FILES['ImageFile']['size'];
$tmp = $_FILES['ImageFile']['tmp_name'];
$path = "public/www/uploads/";
$valid_formats = array("jpg", "png", "gif", "bmp");
$response = '';
if(strlen($name)) {
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats)) {
if($size<(1024*1024)) {
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
if(move_uploaded_file($tmp, $path.$actual_image_name)) {
$response = "<img src='public/www/uploads/".$actual_image_name."?parm=".time()."' class='preview'>";
} else {
$response = "failed";
}
} else {
$response = "Image file size max 1 MB";
}
} else {
$response = "Invalid file format..";
}
} else {
$response = "Please select image..!";
}
Here, $response is a variable that used to get status.
Sounds like you are using some sort of AJAX to call this function.
You might need to find a way to reset the $_FILES array at the end of this function... maybe something like this would help:
$_FILES = array();
Otherwise, because there is no (apparent) page refresh happening after the file upload (as you mentioned,) I'm thinking that the $_FILES variable has no chance of being naturally reset (as would happen if you weren't using AJAX here.)