这是我的HTML:
<!DOCTYPE html>
<html>
<body>
<form action="uploaded_img.php" method="post" enctype="multipart/form-
data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
PHP代码:
<?php
if(isset($_POST['submit'])){
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if(isset($name)){
if(!empty($name)){
$location = 'uploads/';
if(move_uploaded_file($temp_name, $location.$name)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
}
?>
我的代码给了我这些错误:
注意:未定义索引:文件位于 C:\ xampp \ htdocs \ phptutorial \ uploaded_img.php 在第3行 注意:未定义索引:C:\ xampp \ htdocs \ phptutorial \ uploaded_img.php中的文件 在第4行上,您应该选择要上传的文件!
我想将上传图像保存在c:xamp \ htdocs \ phptutorials \ uploads
答案 0 :(得分:0)
您应该在文件输入元素上使用name
后面的值,例如fileToUpload
而不是file
,
$name = $_FILES['fileToUpload']['name'];
$temp_name = $_FILES['fileToUpload']['tmp_name'];
答案 1 :(得分:0)
在代码中添加文件的正确名称。 像这样
<?php
if(isset($_POST['submit'])){
$name = $_FILES['fileToUpload']['name'];
$temp_name = $_FILES['fileToUpload']['tmp_name'];
if(isset($name)){
if(!empty($name)){
$location = 'uploads/';
if(move_uploaded_file($temp_name, $location.$name)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
}
?>
答案 2 :(得分:0)
尝试一下
rand()