move_uploaded_file not working in wordpress folder - php

I am trying to upload the file inside wordpress folder (wp-content/themes/twenty-twelve/uploads/) it is failing everytime when I submit the form.
My form path (wp-content/themes/twenty-twelve/template-parts/form.php)
<form id="Form" method="post" action="/checkout/" enctype='multipart/form-data'>
<input type="file" name="image">
<input type="submit" name="submit" value="Proceed to Payment" id="FormSubmit" />
</form>
Checkout.php path(wp-content/themes/twenty-twelve/template-parts/checkout.php)
if(isset($_POST['submit'])){
$Image = $_FILES['image']['name'];
$Image = implode(",",$_FILES['image']['name']);
$thisFile = $_FILES['image'];
$Image = $_FILES['image']['name'];
$FileTmp = $thisFile['tmp_name'][0];
$sFileTypeArr= explode(".",$Image );
$sFileType = end($sFileTypeArr);
$randNo = date("ydhis");
$ImageFinal = "IMG_".$randNo.".".$sFileType;
$url = '/wp-content/themes/twenty-twelve';
$fileSavePath = $url."/uploads/";
$ImageFinalWithPath = $fileSavePath.$ImageFinal;
$upload_file = move_uploaded_file($_FILES['image']['tmp_name'], $ImageFinalWithPath);
var_dump($upload_file);
}
Error: bool(false)
NOTE: I have checked the file permission, given full permission. I have also checked the max_file_size_upload and other setting of php.ini.
I am trying to upload one file at a time. It shows the error bool(false)

You should use WordPress helpers
wp_upload_dir()
Get an array containing the current upload directory’s path and url.
See wp_upload_dir() and Determining Plugin and Content Directories.

Related

How to get a input from user to use as url path directory in php

i am working on a upload page for customers to upload some files into wordpress site. The point of doing that is get some numbers from users as variable and create the folder by using that variable. Here is my code, it actually creates a folder and puts the image inside the folder successfuly. What i want is if user inputs "5389" as number, than folder should be uploaded in "/wp-content/useruploads/5389/image.png"
here is my code
<?php /* Template Name: Create Custom Upload Path */ ?>
<?php
global $wp_filesystem;
WP_Filesystem();
$content_directory = $wp_filesystem->wp_content_dir() ;
$wp_filesystem->mkdir( $content_directory . 'useruploads' );
$target_dir_location = $content_directory . 'useruploads/';
if(isset($_POST["submittheform"]) && isset($_FILES['fileToUpload'])) {
$name_file = $_FILES['fileToUpload']['name'];
$tmp_name = $_FILES['fileToUpload']['tmp_name'];
if( move_uploaded_file( $tmp_name, $target_dir_location.$name_file ) ) {
echo "File was successfully uploaded";
} else {
echo "The file was not uploaded";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submittheform">
</form>
i tried to have one more post method and get build number, then use it as variable but was not able to make it happen. any suggestions are appreciated

Using PHP code and rename an image file before uploading to XAMPP database?

I am new to php and is it possible to rename an image file before uploading to the database?
EDIT: would be using a form to upload the file to a database.
<input type="file" name="image">
<input type="submit" name="upload" value="Add" action="viewpage.php">
EDIT: IMAGE OF DATABASE:
The second image above still shows the original image file name in the database while the image name in the directory already changed.
Yor can try this..
<?php
if(isset($_POST['submit_btn']))
{
$tmp_file = $_FILES['uploadedfile']['tmp_name'];
$ext = pathinfo($_FILES["uploadedfile"]["name"], PATHINFO_EXTENSION);
$rand = md5(uniqid().rand());
$post_image = $rand.".".$ext;
move_uploaded_file($tmp_file,"../post_imgs/".$post_image);
}
?>

Image not uploading to web server

I am having some difficulty uploading an image to a folder on my web server. Here's my code:
HTML:
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="Upload Image" name="upload">
</form>
PHP:
<?php
if (isset($_POST["upload"])) {
$name = $_FILES["image"]["name"];
$type = $_FILES["image"]["type"];
$size = $_FILES["image"]["size"];
$temp = $_FILES["image"]["tmp_name"];
$error = $_FILES["image"]["error"];
$target_file = "/profiles/images/$name";
move_uploaded_file($temp, $target_file);
}
?>
I've tried echoing out the file name ($name), but it doesn't return anything at all. It's blank for some reason. This is when I try to upload an image. When I echo $target_file, I get this "/profiles/images/", the $name part is not included for some reason.
Check your max upload file size. If in the settings the size is smaller than the file you are trying to upload, nothing is send.
This is happens in those cases when we try to upload very heavy size image, Please try to check your maximum file size, and please for testing try to upload maximum 100kb size of pic through this code. Hope this will work.

make a upload field using php and send the url to database

I want to create a form to submit an image file in the front-end and send it to the wordpress upload directory and database using php ,but my php says the file is a image but dont upload it :
<div id="recibo">
<form action="" method="post" enctype="multipart/form-data">
<p>upload do recibo:
<input type="file" name="pictures[]" />
<input type="submit" value="Enviar" />
</p>
</form>
</div>
php:
<?php
$target_dir = "wp-content/uploads/recibos";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
There are a few things wrong here.
Firstly, the file input's name attribute is name="pictures[]" yet you are using $_FILES["fileToUpload"], those must match.
So rename it: name="fileToUpload[]"
Sidenote: Using brackets implies an array to upload multiple files; you don't need it here since the rest of your code doesn't support multiple files uploading.
Change it to name="fileToUpload"
Then your folder path:
$target_dir = "wp-content/uploads/recibos";
You have no directory seperator after recibos
This would translate to recibosFILE.JPG rather than recibos/FILE.JPG.
Change it to:
$target_dir = "wp-content/uploads/recibos/";
Make sure the folder you are uploading to has the correct permissions to write to it.
Another thing: I can't see how if(isset($_POST["submit"])) {...} would work here, since your submit button does not have the name attribute to match it with.
<input type="submit" value="Enviar" /> to <input name="submit" type="submit" value="Enviar" />
Special note:
If you do want to upload multiple files, then you will need to find yourself another script to do that and this isn't what your question is about.
See also:
PHP change the maximum upload file size
to increase upload size
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.

Php upload image to directory not working

I know there's questions on this issue but I'm trying to get images uploaded to a directory using move_uploaded_file. I'm using the example from the php manual http://www.php.net/manual/en/function.move-uploaded-file.php but I'm getting an Undefined index: file. File is the name of my file input type so I'm not sure why it's undefined.
Here is my form:
<form action="MoveImages.php" class="dropzone" id="my-awesome-dropzone" method="post">
<input type="file" name="file" />
<input TYPE="submit" name="upload" title="Add Images" value="Add Photo"/>
</form>
Here's my PHP file:
<?php
$uploads_dir = '/Users/Jane/Desktop/NE';
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
?>
Can anyone see where I am going wrong?
Add enctype="multipart/form-data" in your form tag.
That attribute is required when you are uploading file.

Categories