Reputation: 1312
My html form:
<form action='' method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
My php file:
if ($_POST['submit'] == "Submit") {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("/downloads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/downloads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"];
}
}
there are 2 folders in my public_html: tmp
and downloads
, both 777 permissions (just to test)
this file is located in a .htaccess protected folder within downloads (public_html/downloads/new/update.php) and i want the zip files to be uploaded in the downloads dir.
This code won't give me any errors, but does not upload the file. Why?
Upvotes: 1
Views: 2887
Reputation: 1312
This is how i solved it:
HTML:
<form enctype="multipart/form-data" method="post" action="">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
if($_POST['submit'] == "Submit") {
$filename = $_FILES["file"]["name"];
$source = $_FILES["file"]["tmp_name"];
$type = $_FILES["file"]["type"];
$name = explode(".", $filename);
$target_path = "../".$filename;
if(move_uploaded_file($source, $target_path)) {
$message = "Your .zip file was uploaded";
} else {
$message = "ERROR";
}
if($message) echo $message;
}
Upvotes: 0
Reputation: 457
PHP needs MAX_FILE_SIZE to receive uploaded files
<form action='' method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="10240000">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
with this form you can upload a zip archiv with 10 MB or less...
you must also set the max_post_size and upload_max_filesize in your php.ini to the same or a higher value to upload files
use this PHP code
if ((isset($_POST['submit']) && $_POST['submit'] == "Submit") AND isset($_FILES)) {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("/downloads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
if(move_uploaded_file($_FILES["file"]["tmp_name"],
"/downloads/" . $_FILES["file"]["name"]))
echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"];
else echo "file could not be processed";
}
}
Upvotes: 1
Reputation:
Try adding
if(move_uploaded_file(...)){
echo "it works";
} else {
echo "NOPE";
}
And replace:
move_uploaded_file($_FILES["file"]["tmp_name"], "/downloads/" . $_FILES["file"]["name"]);
with:
move_uploaded_file($_SERVER['DOCUMENT_ROOT'].'/'.$_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT']."/downloads/" . $_FILES["file"]["name"]);
Upvotes: 2