File upload Not Working with PHP - php

So I have a form that allows a user to upload files. When they submit the file, I can get the file information such as name and tmp_name, yet the actually upload doesn't work. I don't get any PHP errors either. Below is my code, I think I just need another pair of eyes on it, as it was working a few days ago.
//Get the file name
$target_Dir = "temp/";
$tempName = $_FILES['file']['tmp_name'];
$target_file = $target_Dir . basename($_FILES["file"]["name"]);
$filename = pathinfo($target_file, PATHINFO_FILENAME);
//Get the password
$password = $_POST['password'];
//Store if the user wants the certificate to remain password protected
$passProtect = $_POST['passProtect'];
//upload the file to the server
move_uploaded_file($tempName, $target_file);
I need the file name without the extension for a later point in my code, in case you're wondering why I'm storing the file name without the extension.

I think you just did this mistake
$fileExtension = pathinfo($target_file, PATHINFO_FILENAME);
Try using this insted of that Code then only you will get the file name not the extention.

I had an unlink() further down in my script for some reason that I can't remember. It deleted the file that the user would upload right away.
Thanks for the extra set of eyes guys.

Related

PHP validation fails when executing on another computer, but same browser, same script, same server, same permissions

I have lost many nights of sleep because of this. Any help is very much appreciated.
There is a very simple validation in my .php file, which checks for the file extension, after the user uploads a file through a form.
$allowed = array('png','jpg','rar','zip');
$filename = $_FILES['userfile']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed))
{
$errorMessage .= 'File is not of type png, jpg, rar or zip';
$validData = false;
}
When I access the page of the form and submit a file, the form works as it should. It throws error if I don't upload a file with the correct extension, and success otherwise.
But when anybody else, from another computer, submits a file in the form, it ALWAYS throws error. I cannot understand what is happening.
As I have mentioned in the question title, the server, browser, script are all the same. And the rest of the form (name, faculty, facebook link etc) transfers correctly, no errors!
Note: the environment is live.
Use explode to Fetch the file extension
Try this:
$name= $_FILES['file']['make'];
$ext = explode('.',$name);
$fileExt= end($ext);
Now you can check if $fileExt is in the allowed extension array
if (in_array($fileExt, $allowed))
This should get the job done irrespective of the machine

php move_uploaded_file not creating file

I am having a problem with move_uploaded_file().
I am trying to upload a image path to a database, which is working perfectly and everything is being uploaded and stored into the database correctly.
However, for some reason the move_uploaded_file is not working at all, it does not produce the file in the directory where I want it to, in fact it doesn't produce any file at all.
The file uploaded in the form has a name of leftfileToUpload and this is the current code I am using.
$filetemp = $_FILES['leftfileToUpload']['tmp_name'];
$filename = $_FILES['leftfileToUpload']['name'];
$filetype = $_FILES['leftfileToUpload']['type'];
$filepath = "business-ads/".$filename;
This is the code for moving the uploaded file.
move_uploaded_file($filetemp, $filepath);
Thanks in advance
Try this
$target_dir = "business-ads/";
$filepath = $target_dir . basename($_FILES["leftfileToUpload"]["name"]);
move_uploaded_file($_FILES["leftfileToUpload"]["tmp_name"], $filepath)
Reference - click here
Try using the real path to the directory you wish to upload to.
For instance "/var/www/html/website/business-ads/".$filename
Also make sure the web server has write access to the folder.
You need to check following details :
1) Check your directory "business-ads" exist or not.
2) Check your directory "business-ads" has permission to write files.
You need to give permission to write in that folder.
make sure that your given path is correct in respect to your current file path.
you may use.
if (is_dir("business-ads"))
{
move_uploaded_file($filetemp, $filepath);
} else {
die('directory not found.');
}

Getting error when renaming file in PHP

I am uploading a file using PHP to a folder in my directory and am unable to rename it using the following code
$da = date("dmY");
$ja = $uid.$da;
$mukesh = $app.$ja;
// If no errors, upload the image, else, output the errors
if($err == '') {
if(move_uploaded_file($_FILES['userfile'][$mukesh], $uploadpath));
Here's PHP's official document about how to handle uploads: http://www.php.net/manual/en/features.file-upload.post-method.php
The method move_uploaded_file() requires two parameters, a filename of the temp file, and a new location.
$tmp = $_FILES['userfile']['tmp_name']; // temp path
move_uploaded_file($tmp, $uploadpath . '/' . $mukesh);
You will need to name your input element userfile.
<input type="file" name="userfile" />
Based on code snippet provided, you can do following
move_uploaded_file ($_FILES["userfile"]["tmp_name"], $uploadpath);
When you upload a file, files will be store in upload location specified in php.ini using a temporary name. This file location with name can be accessed by $_FILES["userfile"]["tmp_name"]
Lets say you upload an image.if no error then
$uploads_dir = 'as per you defined';
$tmp_name = $_FILES["userfile"]["tmp_name"];
$name = 'custom_file_name.png';//$_FILES["userfile"]["name"];
move_uploaded_file($tmp_name, $uploads_dir."/".$name);
You are renaming the temp name of file ...
When you want to rename the change the name with which you want to store the file
$filename = time().$_FILES['userfile']['name'];
$upload_path = 'path_to_ur_upload_folder'.$filename;
move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path );
first param in move_upload_file is temporary name that will be used by stream while copying an d uploading .. the second parameter is path where your file will be saved (along with file name).. it is the second parameter which will help you in renaming of file which is being uploaded

Create destination folder on file upload with same name as filename

I've been looking at various PHP/AJAX file upload plugins online, but I'm having some trouble finding one feature I really need. For the purposes of this upload, each file that I upload must go into a directory with the same name as that file (minus the extension). Naturally, the nicest way would be to create that folder during the upload and then send the file to it. I know this involves mkdir() in some way, and I've found a number of scripts that even perform basic folder creation, but I'm not clear on how to do this dynamically using the filename. Any ideas?
Thanks!
When you upload a file in php it is stored in the $_FILES array, its name is stored in $_FILES['inputfield']['name'] where 'inputfield' is the name in the file input like:
<input type='file' name='inputfield' />
So then you would do:
$exp = explode(".",$_FILES['inputfield']['name']);
$filename = $exp[0];
$path = "/path/to/base/folder/" . $filename . "/" . $_FILES['inputfield']['name'];
move_uploaded_file($_FILES['inputfield']['tmp_name'], $path);
$fileName = $_FILES['fieldname']['name']
$foldername = substr($fileName, 0, strrpos($fileName, '.'));

how to rename file name while uploading

I have a file with 2 input fields one for file name (user will type) and the second one for choosing file what I want to upload the file to a directory with the name user typed.
down is the code I'm using guys please help me how to change the file name into what user typed.
<?php
$filename = $_POST["file"]
$upload = $_FILES['userfile'];
$target_path = "upload/";
$target_path .= $upload["name"];
$newname = "anything";
if(move_uploaded_file($upload["tmp_name"], $target_path))
{
echo "uploaded successfully";
}
?>
Change $target_path .= $upload["name"]; to something like $target_path .= $filename;.
edit: For the record, I have to say that letting people upload files (and choose the extension) to your web server raises some serious security concerns. I would suggest at least disabling the ability to execute scripts in your target folder.

Categories