我正在编写一张用于上传照片的表格,但是它不起作用。这是我的代码:
HTML:
<!DOCTYPE html>
<html>
<body>
<form action="akcija.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload">
<input type="submit" value="Ucitaj" name="submit">
</form>
</body>
</html>
PHP:
?php
$fileName = $_FILES["fileToUpload"]["name"];
$path = "/htdocs/";
$newFilePlace = $path . $fileName;
$tmpname = $_FILES["fileToUpload"]["tmp_name"];
echo $fileName . " nejm" . "<br/>" ;
echo $newFilePlace . "<br/>";
echo $tmpname . " hej <br/>";
if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newFilePlace)) {
echo "ERROR";
}
?>
输出:
800px-Juan_Manuel_Santos_and_Lula_(cropped).jpg nejm
/ htdocs / 800px-Juan_Manuel_Santos_and_Lula_(cropped).jpg / tmp / phpAQPhC7
hej错误
答案 0 :(得分:1)
仅添加文件夹的根路径,并在文件所在的文件夹内创建一个名为htdocs的文件夹。
define ('SITE_ROOT', realpath(dirname(__FILE__)));
$fileName = $_FILES["fileToUpload"]["name"];
$path = SITE_ROOT; // for root folder
$path1 = SITE_ROOT . "foldername"; // for specific folder
$newFilePlace = $path . $fileName;
$tmpname = $_FILES["fileToUpload"]["tmp_name"];
echo $fileName . " nejm" . "<br/>" ;
echo $newFilePlace . "<br/>";
echo $tmpname . " hej <br/>";
if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newFilePlace)) {
echo "ERROR";
}
这已经过测试,对我来说效果很好
答案 1 :(得分:0)
好吧,如果您想将该文件上传到htdocs,请记住将该路径用作空字符串,请记住您的文件也存在于htdocs目录中。这段代码适合您。
<!DOCTYPE html>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload">
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
<?php
if(isset($_POST["submit"])){
$fileName = $_FILES["fileToUpload"]["name"];
$path = "";
$newFilePlace = $path . $fileName;
$tmpname = $_FILES["fileToUpload"]["tmp_name"];
echo $fileName . " nejm" . "<br/>" ;
echo $newFilePlace . "<br/>";
echo $tmpname . " hej <br/>";
if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newFilePlace)) {
echo "ERROR";
}
}
?>