Hi,
i am having problem uploading.

1.

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

2.

<?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"];
  }
?>

3. i can browse and upload the file. But i get below screen, when i click submit.

0)

{echo "Error: " . $_FILES["file"]["error"] ."
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . " 
  echo "Type: " . $_FILES["file"]["type"] . "
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb ";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }

I am new. Do i need to create a folder so attached file can be saved?

Thanks.

Recommended Answers

All 7 Replies

I'm willing to talk about this to you. inside your saved where the php program and the html file was saved, you must create a folde of the name you want. and then what ever is the name of your folder is the same as the folder in the variable...

$name_of_file=$_FILES['name'];
$tmp_name=$_FILES['tmp_name'];
$path="name_of_your_created_folder/".$name_of_file
if($error==UPLOAD_ERR_OK) //checks the upload if it is ok
{
move_uploaded_file($tmp_name,$path);
}
else{
echo "upload error";
}

Thanks.

I created a folder "upload". i did not create a file inside the folder.
i had the below code. i dont get error message but i get blank scree after i click submit. then i went to folder UPLOAD but attached file was not there.

<?php
$name_of_file=$_FILES['name'];
$tmp_name=$_FILES['tmp_name'];
$path="upload/".$name_of_file
if($error==UPLOAD_ERR_OK) //checks the upload if it is ok
{
move_uploaded_file($tmp_name,$path);
}
else{
echo "upload error";
}
?>

pls ADVISE.

Colon ';' is missing at line 4. Also, try with realpath of your upload folder. Change the '$path' like below:

$path = $_SERVER['DOCUMENT_ROOT'] . "/your_web_folder/upload/" . $name_of_file;

Don't miss the colon ';' at the end of the line.

Hi,

i had the below code. i get blank screen after i hit submit. then i look in the upload folder i see nothing in there. i created upload folder. but i did not do the name of file.

$path = $_SERVER['DOCUMENT_ROOT'] . "/your_web_folder/upload/" . $name_of_file;

Should i keep $name_of_file or i should change it to something?

my code i ran:

<?php
$name_of_file=$_FILES['name'];
$tmp_name=$_FILES['tmp_name'];
$path = $_SERVER['DOCUMENT_ROOT'] . "/sqlPractice/upload/" . $name_of_file;
if($error==UPLOAD_ERR_OK) //checks the upload if it is ok
{
move_uploaded_file($tmp_name,$path);
}
else{
echo "upload error";
}
?>

Thx.

@bangla, You've incorrect variables for file.

$name_of_file=$_FILES;
$tmp_name=$_FILES;

File name array should've key as the html input file name you used, and the values follow 'name, type, size, error, tmp_name' respectively. As below example:

$file_name = $_FILES['upfile']['name'];
$file_size = $_FILES['upfile']['size'];
$file_type = $_FILES['upfile']['type'];
$temp_file = $_FILES['upfile']['tmp_name'];
$status = $_FILES['upfile']['error'];

Try with below:

<?php
$file_name=$_FILES['upfile']['name']; // give the name of HTML input file name as 'upfile'
$tmp_name=$_FILES['upfile']['tmp_name'];
$status = $_FILES['upfile']['error'];
$path = $_SERVER['DOCUMENT_ROOT'] . "/sqlPractice/upload/";
if(!is_dir($path)) {
//check the directory is already exists, otherwise, create it
mkdir($path, 755);
}
if($status==UPLOAD_ERR_OK) //checks the upload if it is ok
{
move_uploaded_file($tmp_name,$path.$file_name);
}
else{
echo "upload error";
}
?>

Your HTML should've below:

<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="upfile" /><br />
<input type="submit" value="Upload" name="upload" />
</form>

Replace the file name you want to process in "action=''".

Hope this help.

1.

<form method="post" action="upload.php" enctype="multipart/form-data"><input type="file" name="upfile" /><br /><input type="submit" value="Upload" name="upload" /></form><form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="upfile" /><br />
<input type="submit" value="Upload" name="upload" />
</form>

2.

<?php$file_name=$_FILES['upfile']['name']; // give the name of HTML input file name as 'upfile'$tmp_name=$_FILES['upfile']['tmp_name'];$status = $_FILES['upfile']['error'];$path = $_SERVER['DOCUMENT_ROOT'] . "/sqlPractice/upload/";if(!is_dir($path)) {//check the directory is already exists, otherwise, create itmkdir($path, 755);}if($status==UPLOAD_ERR_OK) //checks the upload if it is ok{move_uploaded_file($tmp_name,$path.$file_name);}else{echo "upload error";}?><?php
$file_name=$_FILES['upfile']['name']; // give the name of HTML input file name as 'upfile'
$tmp_name=$_FILES['upfile']['tmp_name'];
$status = $_FILES['upfile']['error'];
$path = $_SERVER['DOCUMENT_ROOT'] . "/sqlPractice/upload/";
if(!is_dir($path)) {
//check the directory is already exists, otherwise, create it
mkdir($path, 755);
}
if($status==UPLOAD_ERR_OK) //checks the upload if it is ok
{
move_uploaded_file($tmp_name,$path.$file_name);
}
else{
echo "upload error";
}
?>

I had a folder upload. I did not create any file inside upload. after i attach the file, i clicked upload. i got blank screen, no error msg. then i double clicked the upload folder. nothing is inside.

Pls advise.

Actually,

#2 =

<?php
$file_name=$_FILES['upfile']['name']; // give the name of HTML input file name as upfile'

$tmp_name=$_FILES['upfile']['tmp_name'];

$status = $_FILES['upfile']['error'];

$path = $_SERVER['DOCUMENT_ROOT'] . "/sqlPractice/upload/";

if(!is_dir($path)) { //check the directory is already exists, otherwise, create it

mkdir($path, 755);

}

if($status==UPLOAD_ERR_OK) //checks the upload if it is k

{

move_uploaded_file($tmp_name,$path.$file_name);
}
else
{
echo "upload error";
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.