I use PHP to upload file to my server.I can't find my file in files folder.I suppose My code is right, but the file php.ini should be configured before uploading the file.Who can help me ? Here is my html code:
<form action="upload_file.php?act=put&name=use_resource" method="post" enctype="multipart/form-data">
<label for="file">PUT:</label>
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
My upload_file.php code is:
<?php
if ($_GET["act"] == "put")
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
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 />";
move_uploaded_file($_FILES["file"]["tmp_name"], "files/".$_FILES["file"]["name"]);
}
}
Your code is perfect and working, Did you give write access to 'files' folder?
Related
I am trying to upload a file that has an Arabic name like (مرحبا بكم)
But when I upload it to server the name is not correct, it shows characters like that (ريÙ-Ù).
So, how can I upload files with Arabic name?
Code:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
PHP File:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
you have to encode the name in unicode.
you could use base64_encode($filename) to save the name of the file
I have tried numerous attempts at this code, i know the html is correct, but the PHP is tricky
<html>
<body>
<?php
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>";
$uploaddir = '/var/www/Megan/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
move_uploaded_file($_FILES["file"]["tmp_name"],
$uploadfile);
echo "Stored in: " . "$uploaddir" . $_FILES["file"]["name"];
?>
<h1>File upload successful!</h1>
<form method="get" action="/megan">
<input type="submit" value="Continue"/>
</form>
</body>
</html>
help me fix this
Hey try this simple code, I think this will help you, and add your code as per your requirement.
<form enctype="multipart/form-data" method="post">
<input type="file" name="file" />
<input type="submit" name="submit" value="upload" />
</form>
<?php
$name=$_FILES['file']['name'];
$temp=$_FILES['file']['tmp_name'];
$dir="var/www/Megan/";
move_uploaded_file($temp,$dir.$name);
?>
To start, your assumption that the HTML is correct is wrong. File upload forms need to be method="POST"
One of your form elements needs to be a file picker:
<input type="file" name="file">
Your opening form tag needs an extra parameter:
<form method="post" enctype="multipart/form-data">
Your PHP should only run if the form has been POSTED, in which case the variable $_POST is set. So put your php code inside a
if($_POST) {
//php here
}
may be your uploading directory is readonly or write protected
<?php
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>";
$uploaddir = '/var/www/Megan/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
chmod($uploadfile, 0777);
if($_FILES['file']['error']==0) {
if(move_uploaded_file($_FILES["file"]["tmp_name"], $uploadfile)){
echo "Stored in: " . "$uploaddir" . $_FILES["file"]["name"];
} else {
echo "error!!";
}
} else {
echo "An error has occurred.<br/>Error Code: " . $_FILES["file"]["error"];
}
?>
Below is my html code....
<form enctype="multipart/form-data" action="some.php" method="POST">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
and my some.php code...
print_R($_FILES);
print_r($_POST);
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_POST["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
$_POST RESULTS IN Array ( [file] => gcc-mlion.tar [submit] => Submit )
BUT $_FILES gives empty result.
When you try to print File array that time your Spell of "print_r" is wrong.
You write "print_R" instead of "print_r", Php is case sensitive so it's matters a lot.
You're trying to output the value of $_POST['file']['name'];. It will return an undefined index error message.
Change that line to:
echo "Upload: " . $_FILES['file']['name'] . "<br>";
That should fix the issue.
Also, here's how I'd do it:
<pre>
<?php
if(isset($_POST['submit'])) //checking if form was submitted
{
print_r($_FILES);
print_r($_POST);
if ($_FILES["file"]["error"] > 0) //checking if error'ed
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES['file']['name'] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
?>
</pre>
<form enctype="multipart/form-data" action="" method="POST">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
Hope this helps!
I have this form:
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
And this php script:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
move_uploaded_file($_FILES["file"]["tmp_name"],
"/tmpupload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/tmpupload/" . $_FILES["file"]["name"];
}
?>
My goal is using curl from command line to upload a file.
The problem is to avoid the interaction with the button submit and choose, so the upload should me automatic.
Is there a solution?
To make your form auto submit on selecting the file change your file input to look like
<input type="file" onchange="formname.submit();" name="file" id="file" />
So make sure you give your form a "name" property and change 'formname' in the onchange to be the same as your chosen form name.
I am trying to upload a file using following code but the file is not getting saved in the desired location and no error is being popped out.
The php code is as follows`
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
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("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/".$_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
?>
The html code is
<html>
<body>
<form action="index.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Check user permissions of the folder you want to store file to.