Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File upload not working | Php

Tags:

file

forms

php

I'm trying to upload a file in php and unable to do it. The file I'm trying to upload is a csv file, but it should not be a concern. I'm using php to upload my file. I'm also trying to process the form in the same page. Below is my code for file upload and it is not working...

<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="file" name="csv_file">
    <input type="submit" name="submit">
</form>


<?php   
    if(isset($_POST['submit'])) {
        if(isset($_POST['csv_file'])) {             
            echo "<p>".$_POST['csv_file']." => file input successfull</p>";
            fileUpload();
        }
    }   

function fileUpload () {
    $target_dir = "var/import/";
    $file_name = $_FILES['csv_file']['name'];
    $file_tmp = $_FILES['csv_file']['tmp_name'];

    if (move_uploaded_file($file_tmp, $target_dir.$file_name)) {
        echo "<h1>File Upload Success</h1>";            
    }
    else {
        echo "<h1>File Upload not successfull</h1>";
    }
}

?>

like image 390
Abhishek Dhanraj Shahdeo Avatar asked Aug 22 '16 06:08

Abhishek Dhanraj Shahdeo


People also ask

Why is my file not uploading?

Insufficient storage space on Google Drive. Unstable network connection with the computer. Google's backup and sync feature doesn't work well. File type of the uploading item is not supported by Google Drive.

What is Tmp_name in PHP file upload?

tmp_name is the temporary name of the uploaded file which is generated automatically by php, and stored on the temporary folder on the server. name is the original name of the file which is store on the local machine.

How can I tell if a file is uploaded in PHP?

The is_uploaded_file() function in PHP is an inbuilt function which is used to check whether the specified file uploaded via HTTP POST or not. The name of the file is sent as a parameter to the is_uploaded_file() function and it returns True if the file is uploaded via HTTP POST.


1 Answers

update your form code with enctype attribute

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype = "multipart/form-data">
    <input type="file" name="csv_file">
    <input type="submit" name="submit">
</form>
like image 80
AmmyTech Avatar answered Sep 25 '22 15:09

AmmyTech