I want to upload a file to a given folder.
<?php
$folder = "upload/";
if (is_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'])) {
if (move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])) {
echo "File uploaded";
} else {
echo "File not moved to destination folder. Check permissions";
};
} else {s
echo "File is not uploaded";
};
?>
The error is:
Notice: Undefined variable: HTTP_POST_FILES in C:\wamp\www\sdg\import\ips.php on line 3
Below is one way to upload files, there are many other ways.
As @nordenheim said,$HTTP_POST_FILES
has been deprecated since PHP 4.1.0, thus not advisable to use so.
PHP Code (upload.php)
<?php
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$allowedTypes = ['jpg', 'png'];
if (isset($_POST["submit"])) {
// check type
if (!in_array($imageFileType, $allowedTypes)) {
$msg = "Type is not allowed";
} // Check if file already exists
elseif (file_exists($target_file)) {
$msg = "Sorry, file already exists.";
} // Check file size
elseif ($_FILES["fileToUpload"]["size"] > 5000000) {
$msg = "Sorry, your file is too large.";
} elseif (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$msg = "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";
}
}
?>
HTML Code to start function
<form action="upload.php" method="post" id="myForm" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
</form>
Hope this helps.
PHP 4.1 introduced the superglobals. They replace the old, long-named arrays that contain the data extracted from the request. replaced
,
$_GET[]
replaced$HTTP_GET_VARS[]
and so on
For subsequent PHP 4 versions the old and the new arrays were available side by side. PHP 5 by default disabled the generation of the old arrays and introduced the
that could be used to re-enable the creation of the old arrays.
Since PHP 5.4 the old long-named arrays were removed completely andregister_long_arrays
went together with them.
Conclusion: You are learning from a very old or very bad tutorial. Find a better one.
First, write your html code like this, and don't forget to addenctype="multipart/form-data"
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
Then create a file name called upload.php
<?php
$path = "form/";
$target_file = $path.basename($_FILES["fileToUpload"]["name"]);
$file=$_FILES['fileToUpload']['name'];
$result = move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_file.$file);
if ($result) {
echo "file successfully uploaded";
}
else {
echo "please select your file";
}
public static function uploadFile($filepath="upload",$existCheck=0,$uniq=0){
global $_FILES;
try {
// Undefined | Multiple Files | $_FILES Corruption Attack
// If this request falls under any of them, treat it invalid.
if (
!isset($_FILES['uploaded_file']['error']) ||
is_array($_FILES['uploaded_file']['error'])
) {
$result["status"]="fail";$result["errors"]=('Invalid parameters.');return $result;
}
// Check $_FILES['uploaded_file']['error'] value.
switch ($_FILES['uploaded_file']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
$result["status"]="fail";$result["errors"]=('No file sent.');return $result;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$result["status"]="fail";$result["errors"]=('Exceeded filesize limit.');return $result;
default:
$result["status"]="fail";$result["errors"]=('Unknown errors.');return $result;
}
// You should also check filesize here.
if ($_FILES['uploaded_file']['size'] > 1000000) {
$result["status"]="fail";$result["errors"]=('Exceeded filesize limit.');return $result;
}
// DO NOT TRUST $_FILES['uploaded_file']['mime'] VALUE !!
// Check MIME Type by yourself.
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['uploaded_file']['tmp_name']),
array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
),
true
)) {
$result["status"]="fail";$result["errors"]=('Invalid file format.');return $result;
}
if($uniq==0){
$temp=$filepath;
}
else{
$temp=$filepath."/".uniqid()."_".$_FILES['uploaded_file']['name'];
}
if ($existCheck==1 && file_exists($temp)) {
$result["status"]="fail";$result["errors"]=('Unknown errors.');return $result;
}
if(@copy($_FILES['uploaded_file']['tmp_name'], $temp)) {
return $result["status"]="success";
}
$result["status"]="fail";$result["errors"]=('Unknown errors.');return $result;
} catch (Exception $e) {
$result["status"]="fail";$result["errors"]= $e->getMessage();return $result;
}
}
You should try this
$fileType = $_FILES['profileimage']['type'];
$fileName = $_FILES['profileimage']['name'];
$array = explode(".", $fileName);
$ext = $array[count($array)-1];
$imageAutoName = "profileimagehr".$rsinssupp.".".$ext;
if(!move_uploaded_file($_FILES['profileimage']['tmp_name'],'img/user/'.$imageAutoName))
{
$msg = "";
}
else
{
$iquery = "UPDATE tblname SET filane = '".$imageAutoName."' WHERE id = ".$rsinssupp."";
$obj->edit($iquery);
}
index.php
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_tmp =$_FILES['image']['tmp_name'];
$extensions= array("jpeg","jpg","png");
move_uploaded_file($file_tmp,"images/".$file_name);
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
create one images folder in your project folder and run this file.
Hi you can use phpUpload class
$pUp = new phpUpload($_FILES['file']);
//$pUp->maxSize('1024');
//$pUp->allowedType(["image/jpeg", "image/png"]);
//$pUp->newName("New_name");
$pUp->run('destination/folder'); // Move the file to a specific folder
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.