$_FILES is empty but move_uploaded_file() works - php

I have the next code:
<?php
if(empty($_FILES))
echo 'vacia';
else
echo 'con algo';
var_dump($_FILES);
$tempFile = $_FILES['Filedata']['tmp_name'];
$fileName = $_FILES['Filedata']['name'];
move_uploaded_file($tempFile, "./" . $fileName);
?>
Now, empty($_FILES) is true because shows me the string 'vacia'. Also show me 'array(0) {}' in var_dump().
But move_uploaded_file() still works perfectly. Could someone explain me that?
the error_reporting(E_ALL) say 'Undefined index Filedata' in each line where I use it, but I can understand why move_uploaded_file() still works :/

You should check the return of move_uploaded_files():
$result = move_uploaded_files($tempFile, "./", $fileName);
It will be true on success or false on error. In your case, it should be false. Since the array indices do not exist, PHP will throw an E_NOTICE level error and use an empty string, so you're actually doing this:
move_uploaded_file('', "./", '');
But without E_NOTICE enabled, there will be no visible error message. (see http://php.net/manual/en/function.error-reporting.php)

You might want to check to see if the file exists instead. Just because the name exists doesn't mean the file exists.
if(is_file($_FILES['Filedata']['tmp_name'])){
echo 'vacia';
}else{
echo 'con algo';
exit("File doesn't exist, I can't move it so I'm out!");
}
var_dump($_FILES);
$tempFile = $_FILES['Filedata']['tmp_name'];
$fileName = $_FILES['Filedata']['name'];
move_uploaded_file($tempFile, "./" . $fileName);

Related

move_uploaded_file not working despite correct path and existing file

I'm really struggling with an issue relating the move_uploaded_file function of php.
The path should be correct, but see yourself:
$imgDir = "../img/".$CATEGORY;
$fileName = $_FILES['image']['name'];
$maxsize = 800;
$compQuality= 75;
if(!is_dir($imgDir)) {
mkdir($imgDir.'/tn', 0777, true);
chmod($imgDir, 0777);
chmod($imgDir, 0777);
}
$imgDir = $imgDir."/";
$imgTnDir = $imgDir."tn/";
I have also tested it by echo-ing everything and it seems to work, but when it comes to the move_uploaded_file it still does not work, even though I test if the file really exists:
if(file_exists($imgDir.$fileName)) {
$status = "The file ".$fileName." already exists, please choose a different title.";
}
if(!move_uploaded_file($_FILES['image']['tmp_name'], $imgDir.$fileName)) {
$status = "File upload failed, sorry.";
}
if(!empty($status)) {
echo $status;
exit();
}
I hope that someone can help me. If you want I can print anything out you need or give you more snippets of the code. The var $_FILES['image']['tmp_name'] does not only exist, but has a proper name by the way.
Also I have checked the php.ini and both uploading is allowed and the size is surely set high enough.
Thank you in advance.
make sure that you have the right path in $imgDir. You can try:
if(!is_writable($imgDir)) { exit('CANNOT_WRITE_IN_DIR'); }
and also you can use an absolute path, like this:
$imgDir = __DIR__ . "/../img/".$CATEGORY;

php - uploading file using doesn't give error

what i want to do is to upload image to the server using php
this is my code
<?php
try {
$name = isset($_POST['variable2']);
$file = rand(1000,100000)."-".isset($_FILES['file']['name']);
$file_name = isset($_FILES['file1']['name']);
$file_loc = isset($_FILES['file1']['tmp_name']);
$file_size = isset($_FILES['file1']['size']);
$file_type = isset($_FILES['file1']['type']);
$folder="uploads123/";
$new_size = $file_size/1024;
$new_file_name = strtolower($file);
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
echo "good";
}else{
echo "error";
}
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
these are my problems
why is it that the output it just the "error" from the if statement? it doesn't give details about the error even if i'm using try and catch.
even if i give a wrong foldername(the directory where the image to be uploaded) it just give me output like "error" from the if statement.
it doesn't give error that the folder doesn't exist on the server.
thank you.
Your html form is like that?
<form action="target.php" enctype="multipart/form-data">
<!-- Content -->
</form>
Does folder exists? check privilegies on server for folder
Good way before move file:
$upload_dir = '/uploads123';
if(!is_dir($upload_dir)){
mkdir($upload_dir, 0777); // you may set your access rule
}
Then you can try move file to this dir
EDIT: Try changing PDOException to Exception
isset() returns a boolean (true or false)
$file_loc will not be a valid file location, but is a boolean:
$file_loc = isset($_FILES['file1']['tmp_name']); // $file_loc = true
move_uploaded_file wil fail as it has no valid file to move and returns false. Your code will echo "error" as a result of move_uploaded_file returning false.
There is no Exception thrown, let alone a PDOException.

uploading binary file to server

I'm converting an image to a binary file in IOS, which works just fine. This will be handled by my php script which is suppose to upload this image to my ubuntu server. The problem is i keep getting file=unsuccessful. i've tried different directory paths, but cant seem to solve this issue.
This $directory will return this: /var/www/User/core/ios/
<?
if(!empty($_POST))
{
$message = $_POST['message'];
$directory = $_SERVER['DOCUMENT_ROOT'] . '/User/core/ios/';
$file = basename($_FILES['userfle']['upload']);
$uploadfile = $directory . $file;
var_dump($_FILES);
$randomPhotoID = md5(rand() * time());
echo 'file='.$file;
echo $file;
if (move_uploaded_file($_FILES['userfle']['tmp_name'], $uploadfile)) {
echo 'successful';
}
else
{
echo 'unsuccessful';
}
}
else
{
echo('Empty post data');
}
?>
Check the error file of your php(you can make sure if you enabled the error log in php.ini),
if you don't have the permission or for some other reasons it can't move the file ,there will be a record in that file.
Some time you can try the command setenforce 0 if you confirm you(I means the user of apache) have the permission to move the file but it not work.
By the way if the file you want to move is not upload by post, there is no error log and the move function will return false.

PHP simple file upload

I'm doing a simple file upload using the following script:
$errors = '';
$target_path = "[PATH HERE]";
$target_path = $target_path . basename($_FILES['uploadFile']['name']);
if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target_path)) {
$errors = "The file ". basename( $_FILES['uploadFile']['name']). " has been uploaded";
} else{
$errors = "There was an error uploading the file, please try again! Type: " . $_FILES['uploadFile']['type'];
}
For some reason, I get an error uploading the file and the file type is not displayed. It seems to only grab the name of the file without the extension (i.e. "test" rather than "test.pdf"). I'm sure it's something simple, but what am I doing wrong?
If you check the error element in the files array, you'll probably find it's some value other than 0. The error should be 0 if nothing went wrong. Otherwise, compare the value stored in error against the PHP documentation to determine what went wrong.
Perhaps your entering the path wrong (ending slash), or php dont have permission to write to the directory.
<?php
error_reporting(E_ALL); // Show some errors
$target_path = "/var/www/somesite.com/uploads/"; // Would require a ending slash
$target_path = $target_path.basename($_FILES['uploadFile']['name']);
if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target_path)) {
$errors = "The file ". basename( $_FILES['uploadFile']['name']). " has been uploaded";
} else{
$errors = "There was an error uploading the file, please try again! Type: " . $_FILES['uploadFile']['type'];
}
?>

How to test if a directory already exist in PHP?

How can i test if a directory already exist and if not create one in PHP?
Try this:
$filename = "/tmp";
if (!file_exists($filename))
echo $filename, " does not exist";
elseif (!is_dir($filename))
echo $filename, " is not a directory";
else
echo "Directory ", $filename, " already exists";
file_exists checks if the path/file exists and is_dir checks whether the given filename is a directory.
Edit:
to create the directory afterwards, call
mkdir($filename);
To expand on the answer above based on the questioner's comments:
$filename = "/tmp";
if (!is_dir($filename)) {
mkdir($filename);
}
You need to use mkdir() to actually make the directory.
Try this:
$dir = "/path/to/dir";
if(is_dir($dir) == false)
mkdir($dir);
If you want the complete path the be created (if not present), set recusive parameter to true.
See documentation of mkdir for more information.
Use This:
if(file_exists("Directory path") && is_dir("Directory path"))
{
//Your Code;
}

Categories