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

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.

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

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.

keep last modified date when uploading file

Is there a way to preserve the last modified date when uploading a file via HTTP POST?
I already read that it gets changed when you use copy() (See here).
But in my case, it's already changed in the temp folder.
HTML:
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
PHP:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
echo "Modified: ".date('d/m/Y H:i:s', filemtime($_FILES['fileToUpload']["tmp_name"]));
?>
The output is: Modified: 17/02/2016 09:02:39
But the file is actually last edited on 10/02/2016 09:34:23
Properties: (created, modified, access)
Is there a way to prevent that?
The last modified date can be captured in the browser using the File.lastModified property. You can use JavaScript to set the value of a hidden input element to this date. Once the form is submitted, read the timestamp from the hidden input and use the method touch to set the timestamp on the newly created file on the server-side.
https://developer.mozilla.org/en-US/docs/Web/API/File/lastModified
https://www.php.net/touch
Sorry you cannot keep the file information. The uploaded file is considered as new file.

Image upload failing in PHP

So I'm trying to set up an image upload from a form via php on my localhost and after running the code and connecting to the database okay, I'm getting the error for the upload. Since all of the other parts of the form are working after a section by section check, I'll just add the html for the upload input and its relevant php script.
<input type="file" name="image" id="image-select" />
And the portion of the php that has to deal with the image upload and verification after upload:
$image = $_FILES ['image']['name'];
$type = #getimagesize ($_FILES ['image']['tmp_name']);
//Never assume image will upload okay
if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['image']['error']);
}
//Where the file will be placed
$target_path = "uploads/";
// Add the original filename to target path
$path= $target_path . basename($image);
// Check if the image is invalid before continuing
if($type === FALSE || !($type[2] === IMAGETYPE_TIFF || $type[2] === IMAGETYPE_JPEG || $type[2] === IMAGETYPE_PNG)) {
echo '<script "text/javascript">alert("This is not a valid image file!")</script>';
die("This is not a valid image file!");
} else {
move_uploaded_file($_FILES['image']['tmp_name'], $path);
}
So error appears once the code hits the upload process. I was attempting to upload a small PNG file The relevant code I added is also in their respective orders when they appear in the script.
can you please put error here. If error means that after submit page file is not being upload. then please check your form enctype. see below an example
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image-select" />
<input type="submit" value="Submit">
</form>
Just a wild stab in the dark here as you haven't provided your form tag but you have remembered the enctype attribute haven't you?
Sorry I don't have 50 reputation otherwise I would ask this as a comment.
<form enctype='multipart/form-data' action=''>
<input type="file" name="image" id="image-select" />
</form>
Also you should check if the file uploaded OK before calling getimagesize() (not that this would be the source of your issue)
if you are using PHP5 and Imagick, then you can try to use something like the following:
$image->readImageFile($f);

Uploading files on Server

I am writing an app from which user will upload files on the server. I have found a php script from internet but I don't know how am I going to tell the script where to upload the data. This might be a silly question but I am no PHP programmer. I am using this php script in my java code.
Here is the script.
<?php
$filename="abc.xyz";
$fileData=file_get_contents('php://input');
echo("Done uploading");
?>
Regards
This is a terrible way of uploading files, you are much better off using a form and the $_FILES superglobal.
Take a look at the W3Schools PHP File Upload Tutorial; please read all of it. For further reading take a look at the PHP Manual pages on file upload.
The file input type will create the upload box in the html form:
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
After error checking and validating that the file is what you are expecting (very important: allowing users to upload anything to your server is a huge security risk), you can move the uploaded file to your final destination on the server in PHP.
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/abc.xyz");
The filename is actualy the file path along with the name of the new file, set the path there and a file will be created with write permissions.
Make sure you give the servers full path not the relative one and that you have the required permission to create a file there.
Always refer to the PHP Manual
Here's a basic example to get you started:
HTML:
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploaded_file" type="file" />
<input type="submit" value="Upload" />
</form>
</body>
</html>
PHP:
<?php
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
($_FILES["uploaded_file"]["size"] < 350000)) {
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'/upload/'.$filename;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
echo "It's done! The file has been saved as: ".$newname;
} else {
echo "Error: A problem occurred during file upload!";
}
} else {
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
} else {
echo "Error: Only .jpg images under 350Kb are accepted for upload";
}
} else {
echo "Error: No file uploaded";
}
?>
Refer to the documention for more information.
Hope this helps!

Categories