Hey guys I'm trying to make a file uploader on my website and I have followed a tutorial but it doesn't seem to work. I am currently using the following code:
<?php
session_start();
include('../../config/config.php');
if(isset($_SESSION['id'])) {
$filename = $_FILES["file"]["name"];
$filesize = $_FILES["file"]["size"];
$name = $_POST['name'];
$price = "$" . $_POST['price'];
$photo = $_POST['file'];
$description = nl2br($_POST['description']);
$file_ext = getExt($photo);
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"];
}
}
} else {
header("Location: ../../index.php");
}
?>
I seems to create the temporary file fine but I am not sure why I won't move it. Right now I am testing the code with image files.
Also here is my form code:
<form method="post" action="actions/add.php" enctype="multipart/form-data">
</br>
Listing name: <input type="text" name="name" /></br></br>
Listing Photo: <input type="file" name="file"/></br></br>
Price: $<input type="text" name="price"/></br></br>
Information:</br>
<textarea class="add" name="description"></textarea>
</br></br>
<input class="add" type="submit" name="submit"/>
</form>
EDIT:
Some errors:
Warning: move_uploaded_file(upload/Corgi 004.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/stationr/public_html/admin/actions/add.php on line 42
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/chroot/tmp/phpLXObbt' to 'upload/Corgi 004.jpg' in /home/stationr/public_html/admin/actions/add.php on line 42
Thanks for any help.
$filename = $_FILES["file"]["name"];
$filesize = $_FILES["file"]["size"];
$name = $_POST['name'];
$price = "$" . $_POST['price'];
//$photo = $_POST['file'];
$description = nl2br($_POST['description']);
//$file_ext = getExt($photo);
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 />";
$newname = dirname(__FILE__).'/upload/'.$filename;
if (file_exists($newname))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],$newname);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
Related
I'm trying to allow users to upload videos to my website however I can't see where I have gone wrong. I am just trying to get them to upload to a folder first before also adding the file name to my database.
This is my form:
<form action='videoUpload.php' method='post' enctype="multipart/form-data">
<input type='hidden' name='id' value='<?php echo $row['videoID'];?>'>
<p><label>Title</label><br />
<input type='text' name='videoTitle' required value='<?php if(isset($error)){ echo $_POST['videoTitle'];}?>'></p>
<p><label>Image</label><br />
<input type="file" name='video' id="video" required value='<?php if(isset($error)){ echo $_POST['video'];}?>'></p>
<p><input type='submit' name='submit' value='Submit'></p>
and this is my php page:
<?php
require_once('../../../includes/config.php');
if(isset($_POST["submit"])) {
//collect form data
extract($_POST);
$allowedExts = array("ogg", "mp4", "wma");
$extension = pathinfo($_FILES['video']['name'], PATHINFO_EXTENSION);
if ((($_FILES["video"]["type"] == "video/mp4")
|| ($_FILES["video"]["type"] == "video/ogg")
|| ($_FILES["video"]["type"] == "video/wma")
&& ($_FILES["video"]["size"] < 50000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["video"]["error"] > 0)
{
echo "Return Code: " . $_FILES["video"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["video"]["name"] . "<br />";
echo "Type: " . $_FILES["video"]["type"] . "<br />";
echo "Size: " . ($_FILES["video"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["video"]["tmp_name"] . "<br />";
if (file_exists("../videos/" . $_FILES["video"]["name"]))
{
echo $_FILES["video"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["video"]["tmp_name"],
"upload/" . $_FILES["video"]["name"]);
echo "Stored in: " . "../videos/" . $_FILES["video"]["name"];
}
}
}
else
{
echo "Invalid file";
}
}
?>
Maybe it's something really simple that I'm but can't see where I've gone wrong
there are a ) missing on if
if(isset($_POST["submit"])) {
//collect form data
extract($_POST);
$allowedExts = array("ogg", "mp4", "wma");
$extension = pathinfo($_FILES['video']['name'], PATHINFO_EXTENSION);
if ((($_FILES["video"]["type"] == "video/mp4")
|| ($_FILES["video"]["type"] == "video/ogg")
|| ($_FILES["video"]["type"] == "video/wma")
&& ($_FILES["video"]["size"] < 50000000)
&& in_array($extension, $allowedExts))){ //comma missing
if ($_FILES["video"]["error"] > 0)
{
echo "Return Code: " . $_FILES["video"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["video"]["name"] . "<br />";
echo "Type: " . $_FILES["video"]["type"] . "<br />";
echo "Size: " . ($_FILES["video"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["video"]["tmp_name"] . "<br />";
if (file_exists("../videos/" . $_FILES["video"]["name"]))
{
echo $_FILES["video"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["video"]["tmp_name"],
"upload/" . $_FILES["video"]["name"]);
echo "Stored in: " . "../videos/" . $_FILES["video"]["name"];
}
}
}else{
echo "Invalid file";
}
}
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.
<?php
$allowedExts = array("pdf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf") &&
($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts)))
{
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"] / 20000000) . " 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"];
}
}
}
else
{
echo "Invalid file";
}
?>
i am a newbie at php and studying pdf upload . can somebody have an idea what is wrong with my code . i have been researching . thank you so much
<form action="<?php echo $_SERVER["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>
here is my form please somebody help me i am kind of confuse right now .
The problem is, you are posting on the same file which actually generates the HTML.
So when you first generate your HTML, you don't have any posted values, so the script throws this error.
try this :
<?php
if(isset($_FILES['file']){
upload_file();
}else{
?>
<form action="<?php echo $_SERVER["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>
<?php
}
function upload_file(){
$allowedExts = array("pdf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf") &&
($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts)))
{
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"] / 20000000) . " 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"];
}
}
}
else
{
echo "Invalid 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"];
}
?>
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.