Login | Register | FAQ
Anonymous

I'm writing an upload file page

+ Post a reply

4 posts Page 1 of 1


I'm writing an upload file page

by coderadm » Fri Apr 20, 2012 7:08 pm

This is what I have, please keep in mind that I'm a newbie and I'm running WAMP under Windows 2003 Server..

<?php
// To check if the submit has been press or been done
if(isset($_GET['submit'])){
$target_path = "upload/";
$target_path .= basename($_FILES['file_to_upload']['name']);

if(move_uploaded_file($_FILES['file_to_upload']['tmp_name'],$target_path)){
echo "The file has been uploaded";
} else {
echo "There was an error";
}

}

?>


<html>
<head>
<title>Upload file</title>
</head>
<body>
<form method="post" action="#" enctype="multipart/form-data"> <! allows you to upload files -->
<input type="text" name="file_name" placeholder="rename file to:"/><br />
<input type="file" name="file_to_upload" /><br />
<input type="submit" name="submit" value="Upload" />
</form>

</body>
</html>

Any input on what I'm missing will be greatly appreciated. Or point me to right direction would be great.
Thanks a lot in advance
User avatar

coderadm

  • Posts: 3
  • Joined: Tue Oct 04, 2011 9:34 pm
  • Location: Costa Oeste

Re: I'm writing an upload file page

by chrisf1288 » Fri Apr 20, 2012 8:52 pm

Although I haven't worked with PHP uploads to much, I think your code would work better like this:

<?php

$target_path = "upload/";

if(isset($_GET['submit'])){
move_uploaded_file($_FILES['file_to_upload']['tmp_name'],$target_path))
echo "The file has been uploaded";
} else {
echo "There was an error";
}

?>

<html>
<head>
<title>Upload file</title>
</head>
<body>
<form method="post" action="#" enctype="multipart/form-data"> <! allows you to upload files -->
<input type="text" name="file_name" placeholder="rename file to:"/><br />
<input type="file" name="file_to_upload" /><br />
<input type="submit" name="submit" value="Upload" />
</form>

</body>
</html>
User avatar

chrisf1288

  • Posts: 128
  • Joined: Sun Jun 26, 2011 1:01 am

Re: I'm writing an upload file page

by coderadm » Fri Apr 20, 2012 9:36 pm

coderadm wrote:This is what I have, please keep in mind that I'm a newbie and I'm running WAMP under Windows 2003 Server..

<?php
// To check if the submit has been press or been done
if(isset($_GET['submit'])){
$target_path = "upload/";
$target_path .= basename($_FILES['file_to_upload']['name']);

if(move_uploaded_file($_FILES['file_to_upload']['tmp_name'],$target_path)){
echo "The file has been uploaded";
} else {
echo "There was an error";
}

}

?>


<html>
<head>
<title>Upload file</title>
</head>
<body>
<form method="post" action="#" enctype="multipart/form-data"> <! allows you to upload files -->
<input type="text" name="file_name" placeholder="rename file to:"/><br />
<input type="file" name="file_to_upload" /><br />
<input type="submit" name="submit" value="Upload" />
</form>

</body>
</html>

Any input on what I'm missing will be greatly appreciated. Or point me to right direction would be great.
Thanks a lot in advance


I forgot to mention that when I click on the file to upload... nothing happens. I don't get any errors, all it says is 'no file chosen'.

Does anyone know what could be causing this...?
User avatar

coderadm

  • Posts: 3
  • Joined: Tue Oct 04, 2011 9:34 pm
  • Location: Costa Oeste

Re: I'm writing an upload file page

by XainPro » Sat Apr 21, 2012 9:55 am

to upload a file try to learn from this code

Code: Select all
<html>
<body>

<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>

</body>
</html>


PHP Script

Code: Select all
<?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"];
  }
?>
User avatar

XainPro

  • Posts: 3762
  • Joined: Fri Feb 17, 2012 8:10 pm


+ Post a reply

Page 1 of 1