PHP move_uploaded_file fail - php

I am trying to upload an image through alamofire and I have done it! My server received it but when I call move_uploaded_file function in my php file it shows FAILURE. I am sure that my server received the image and i am able to create directories through php. It is just this move_uploaded_file not letting me do the job. Here is my php code:
<?php
if (empty($_FILES["image"])){
$response = array("error" => "no data");
}else{
$path = "./Upload";
if(!file_exists($path)){
mkdir($path,0777,true);
$response["message"] = "new file created";
}else{
$response["message"] = "file already exist";
if(move_uploaded_file($_FILES["image"]["tmp_name"],$path)){
$response["message"] = "You've got it!!!";
}else{
$response["message"] = "upload function fail";
}
}
}
echo json_encode($response);
?>

$path is a directory, not a file. Change it to:
$path = "./Upload/file.txt";

Change:
$path = "./Upload";
With:
$path= "uploads/" . basename($_FILES["fileToUpload"]["name"]);

Related

Move a file from directory to another using PHP

I have a file on one application folder of codiginitor and I want to copy that file to another application folder of codignitor in another directory.
I have tried below code but it doesn't seem to be working:
$file = 'http://xxxxxx/jakson_solar/ftp.php';
$newfile = './mobile_image/transfer/';
if ( copy($file, $newfile) ) {
echo "Copy success!"; die;
}else{
echo "Copy failed."; die;
}
is there another way to copy file from one directory to another directory?
copy requires as first parameter an input file and as second parameter an output file
So do it this way
$file = 'path_to/ftp.php';
$newfile = './mobile_image/transfer/ftp.php';
if ( copy($file, $newfile) ) {
echo "Copy success!"; die;
} else {
echo "Copy failed."; die;
}
#dev please find the code below for your needs
// 1 check if your input from the form is not empty
if ((isset($_FILES['file']))){
// 2. files inside the session
$_SESSION['FILES'] = $_FILES;
// 3. your path
$PATH = $_SERVER["DOCUMENT_ROOT"]."/mobile_image/transfer/";
// 4. temporary name -> optional step but recommended
$TEMP_NAME = explode(".",$_FILES["file"]);
// 5. new name of the file
$NAME_NEW = substr(number_format(time() * rand(),0,'',''),0,20) . '.' .end($TEMP_NAME);
$UPLOAD_FILE = $PATH . basename($NAME_NEW);
// 6. check before moving the file that is not empty
if (isset($UPLOAD_FILE)) {
move_uploaded_file($_FILES['file'], $PATH); }
// 7. custom errors
$json = array();
$json['status'] = "OK";
$json['message'] = 'File moved successfully';
echo json_encode($json);
}else{
// 7. custom errors
$json = array();
$json['status'] = "NO FILE";
$json['message'] = 'No files found';
echo json_encode($json);
}

uploading file from android app into desired folder

I wan to upload file from android app using PHP in specific folder below is the code which I have tried.
pleas help me what is wrong in this code and please suggest me some easy solution for this or is this method right to upload files from android app on serever
$response = array();
if($_SERVER['REQUEST_METHOD']=='POST'){
//checking the required parameters from the request
if(isset($_POST['exp']) && isset($_POST['employee_id']) && isset($_FILES['pdf']['name']) ){
//connecting to the database
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD,DB_DATABASE) or die('Unable to Connect...');
$resume_name = $_POST['exp'];
$employee_id = $_POST['employee_id'];
$file_data = $_FILES['pdf']['name'];
$upload_path = 'Images/Employee_Profile_Picture/'.$employee_id.'/Resume/';
//getting file info from the request
$fileinfo = pathinfo($_FILES['pdf']['name']);
//getting the file extension
$extension = $fileinfo['extension'];
//file url to store in the database
$file_url = $upload_path . getFileName($employee_id). '.'. $extension;
//file path to upload in the server
$file_path = $upload_path . getFileName($employee_id);
try{
if(file_exists($upload_path))
{
$existing_file = glob($upload_path."/*.*");
$empty_file = implode(" ",$existing_file);
move_uploaded_file($_FILES['pdf']['name'],$upload_path) ;
$sql = "UPDATE employee_registration SET resume_name ='$resume_name', resume_path='$file_url' where employee_id ='$employee_id'";
//adding the path and name to database
if(mysqli_query($con,$sql)){
//filling response array with values
$response['Success'] = "File Uploaded Successfully...!";
echo json_encode($response);
}
else
{
$response['Error'] = "File Uploading Error...!";
echo json_encode($response);
}
}
else
{
mkdir('Images/Employee_Profile_Picture/'.$employee_id.'/Resume');
move_uploaded_file($_FILES['pdf']['name'],$upload_path) ;
$sql = "UPDATE employee_registration SET resume_name ='$resume_name', resume_path='$file_url' where employee_id ='$employee_id'";
//adding the path and name to database
if(mysqli_query($con,$sql))
{
//filling response array with values
$response['Success'] = "File Uploaded Successfully...!";
echo json_encode($response);
}
else
{
$response['Error'] = "File Uploading Error...!";
echo json_encode($response);
}
}
}catch(Exception $e){
$response['error']=true;
$response['message']=$e->getMessage();
}
}
}
//here is my method getFileName
function getFileName($employee_id)
{
//mysql query to fetch data
$sql = mysql_query("SELECT resume_path from employee_registration where
employee_id = '$employee_id'") or die(mysql_error());
while ($row = mysql_fetch_array($sql, MYSQL_ASSOC))
{
$response=$row['resume_path'];
}
$resume_name = explode("/", $response);
echo $resume_name[4];
return $resume_name;
}
Change below line:
move_uploaded_file($_FILES['pdf']['name'],$upload_path) ;
To
move_uploaded_file($_FILES['pdf']['tmp_name'],$upload_path) ;
tmp_name should be used to upload the file, as it has the full path of the file where it is temporarily stored. Where as the name contain only the name of file without any path information.
if this doenst work move_uploaded_file($_FILES['pdf']['tmp_name'],$upload_path) ;
kindly use this file_put_contents($upload_path,$_FILES['pdf']['tmp_name']);

could not be able to work with upload file logic in php

I have a php code fragment as
<?php
$target_dir = "uploads/";
$target_file_name = $target_dir .basename($_FILES["file"]["name"]);
$response = array();
// Check if image file is a actual image or fake image
if (isset($_FILES["file"]))
{
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file_name))
{
$success = true;
$message = "Successfully Uploaded";
}
else
{
$success = false;
$message = "Error while uploading";
}
}
else
{
$success = false;
$message = "Required Field Missing";
}
$response["success"] = $success;
$response["message"] = $message;
echo json_encode($response);
?>
when I try to upload a file using Postman I get as error
Warning: move_uploaded_file(uploads/Arshay.png): failed to open stream: No such file or directory in /home/my_domain/public_html/android_api/dum_cv.php on line 9
Warning: move_uploaded_file(): Unable to move '/tmp/phpFPW9sg' to 'uploads/Arshay.png' in /home/my_domain/public_html/android_api/dum_cv.php on line 9
{"success":false,"message":"Error while uploading"}
how to resolve this issue
Try this $target_file_name = $_SERVER['DOCUMENT_ROOT'].'/'.basename($_FILES["file"]["name"]);
It seems that the $target_dir does not exists.
Check the target directory before you move files.
if (!is_dir($target_dir)) {
if (!mkdir($target_dir, 0777, true)) {
exit(json_encode(['success'=>false, 'message'=>'failed to create target directory']));
}
}

move_uploaded_file and file_exists return true, can't view file in browser or FTP

The file does save to the target folder which I can access on the browser and FTP, because when I try to delete the directory it says that the folder is not empty.
I also know they're there, because when I do a scandir(), it lists all the files.
Here is my code:
<?php
$device_name = str_replace(" ","_",strtolower($_POST["device_name"]));
$target_dir = getcwd()."/wedding/";
$target_dir = $target_dir.$device_name."_".time()."_".basename($_FILES["file"]["name"]);
$arr = array();
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)) {
$arr["status"] = 1;
$arr["result"] = "Photo Sent";
} else {
$arr["status"] = 0;
$arr["result"] = "Error Sending Photo";
}
echo json_encode($arr);
?>
Turns out there where special characters in the filename.

Issue in secure file upload script using command line AV

I have a secure file upload function that's part of my website
and I'm using an antivirus to help me checking the file a user trying to upload.
This is my uploadprocess.php file
$target_tmp = "D:\avscan\u\\";
$file = basename( $_FILES['uploaded_file']['name']) ;
if($file != "")
$_SESSION['file'] = $file;
$target = 'C:\xampp\htdocs\ssd\Uploads\\';
$file_path = $target_tmp.$file;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path))
{
$safe_path = escapeshellarg($file_path);
$command = 'scancl'. $safe_path. ' --stdout';
$out = '';
$int = -1;
$output = exec($command, $out, $int);
echo "The output is" .$output;
echo $int;
exit(0);
//Checking for Virus.
if ($int == 0) {
$target = $target.$file;
//echo $target; exit(0);
copy($file_path, $target);
$uploaded = "The file ". $_SESSION['file']. "has been uploaded";
$clean = 'File is Clean.';
$_SESSION['status'] = $clean;
$_SESSION['upload'] = $uploaded;
header("location: ../upload.php");
exit(0);
}
// File is a virus.
else {
$mal = 'Contains Malware';
$deny_up = "Unable to Upload Your File!";
$_SESSION['status'] = $mal;
$_SESSION['upload'] = $deny_up;
header("location: ../upload.php");
exit(0);
}
}
else
{
echo "SORRY, There was a Problem Uploading Your File."; exit(0);
$err_upload = "SORRY, There was a Problem Uploading Your File.";
$_SESSION['err'] = err_upload;
header("location: ../upload.php");
exit(0);
}
It prints me value of 1 for the $int for all files (malicious and non ones) This is my second try with a different AV now I'm using Avira and before I was using clamscan
can someone share me some hints, and tell me what's going on
PS the system is installed on XAMPP if that makes any difference
Can you be more specific about what's not working here? In theory what you doing seems fine at least for ClamAV since it has these return codes (from man clamscan):
RETURN CODES
0 : No virus found.
1 : Virus(es) found.
2 : Some error(s) occured.
Maybe it want to log the output of the exec call, if you are not getting the exit code you expect the reason should be in the output (like missing a command line flag).

Categories