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"];
}
?>
Related
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?
Anyone know how to open and read the uploaded document in the same script in PHP?
I have a form to upload the word file in index.php. Now, I upload the word document. It goes to the file called upload.php and upload it successfully. Now i am wondering that How can write the code for open and read the uploaded file in the same script that means in upload.php
I am very new to PHP. So, please help me and explain me the flow of process detailed. Thanks in advance.
coding for index.php
<html>
<head>
<title>
HTML Converter
</title>
</head>
<body>
<h4>HTML Converter</h4>
<form enctype="multipart/form-data" action="" method="POST">
Please choose a file: <input name="file" type="file" /><br />
<input type="submit" value="Upload" name="submit" />
</form>
</body>
</html>
coding for upload.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"];
$filedata = file_get_contents($_FILES["file"]["name"]);
echo $filedata
;
}
}
You could certainly try something like this for upload.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 {
$new_file = "upload/" . $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"], $new_file);
echo "Stored in: " . $new_file . "<br />";
readfile($new_file);
}
}
On a side-note, if it's a Word file (.doc, .docx, etc), or any binary file for that matter, the output will probably look a little garbled.
Also note that there's no sanitation taking place anywhere, so the code in its current state is probably pretty unsafe (especially if other people besides you personally will be using it). Just a heads-up.
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'm trying to send two forms, one with enctype="multipart/form-data" for file upload, the other without. my problem is only one of them is working. the submit button wont send two form.
What I've tried:
<?php
echo '<form method="post" action="' . $PHP_SELF . '">';
....
echo '<input type="submit" name="submit" value="Submit"><br>';
echo "</form>";
echo '<form action="' . $PHP_SELF . '" 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>';
if (isset($_FILES["file"]["name"])){
$file_name = $_FILES['file']['name'];
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'], '../images/' . $file_name);
}
}
?>
This is how it works in HTML. You can only send one form at a time. The only workaround is to send all your data with just one form.
Just Try With The Following :
<?php
echo '<form method="post" action="' . $PHP_SELF . '">';
....
echo '<input type="submit" name="Submit_Button_One" value="Submit"><br>';
echo '</form>';
echo '<form action="' . $PHP_SELF . '" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="Submit_Button_Two" value="Submit"></form>';
/*First Form Submit*/
if(isset($_POST['Submit_Button_One']))
{
$dataValue = $_POST['data'];
}
/*Second Form Submit*/
if(isset($_POST['Submit_Button_Two']))
{
if(isset($_FILES["file"]["name"])){
$file_name = $_FILES['file']['name'];
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'], '../images/' . $file_name);
}
}
}
?>
I think this may help you to resolve your problem.
Note : Give Different name for submit buttons. Then it's possible to get the datas as you wish from both the forms.
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.