简体   繁体   中英

Uploading a file html/php

I'm currently trying to upload a csv file to a database through simple html and then php. My problem is I like to know how things work/what's actually happening (I guess that's why I have so many questions)

So anyway my HTML is as follows:

<html>
<form action="mysite.php" method="POST">
<input type="file" name="file"><br />
<input type="submit" value="Now upload it!">
</form>
</html>

My PHP is just:

<?php
echo 'Success';
?>

Before I actually store the file in a database of my liking where is the file actually stored?

Is it created as a temp file on the server? Saved on the server ram? Does it not actually do anything since it's not told to do anything (aka: save to DB)

Thanks

Yes it would be in a temp file on the server but in your case it wouldn't because you missed the encoding type :

<form enctype="multipart/form-data" action="__URL__" method="POST">

Here's the manual on file uploading php file uploading

You can see what is uploaded by doing a var_dump($_POST) for variable names and var_dump($_FILES) for file data to help understand.

As you can see in the reference once the file is uploaded you need to move it from its temp location to its final destination.

I highly recommend reading this

Essentially, the file is uploaded via HTTP, and php stores it in a temporary file on the server.

You can get a handle on that temporary file using the global $_FILES array.

When you upload file it is stored on the server, after that if you want to import this CSV to database you must use php script to parse ( CSV Parser ) it and insert in DataBase. Or if you want to store only the file path, not the data from CSV then you must store only file path in DataBase.

The file is stored temporarily as long as the script is running. The information can be found in $_FILES.

The file should be stored to a temporary file on the server, and you can find out information about it using the $_FILES superglobal. You can start out by changing your script to dump out the superglobal to see what's inside:

<?php 
print_r($_FILES); 
?> 

PHP provides a selection of useful functions to deal with file uploads, and you should use these rather than just reaching into the filesystem - remember that any file upload counts as user input and therefore becomes a security risk! You should start by checking if the file is the one you're looking for:

$filename = $_FILES['attachment']['tmp_name'];
if (!is_uploaded_file($filename)) {
    // error
}

And you can use the move_uploaded_file() function to stick the file somewhere more convenient:

if (move_uploaded_file($_FILES['attachment']['tmp_name'], '/more/useful/path') {
    // all good
}

Finally you should remember that not just the file itself, but the filename and the filesize reported, are user-submitted data, and make sure to sanitise these appropriately before use.

There's a tonne of handy stuff here: http://uk.php.net/manual/en/features.file-upload.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM