且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP上传无法正常工作,可能是权限问题

更新时间:2023-02-18 17:42:55

从w3学校开始,他们为示例创建了许多条件.但是我们不需要严格遵循它.我们只有在需要时才能使用它.

As of w3 schools, they have created many conditions for the example. But we don't need follow it strictly. We can use it only if we needed.

这是您可以拥有的最小代码

Here's the Minimal Code that you can have

<!DOCTYPE html>
<html>
<body>
<form action="" 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
if (isset($_POST['submit'])) 
{
echo 'succesfully uploaded';
$structure = 'uploadedfiles/';
$target_file = $structure.basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}
?>

如果使用上述代码,则应在保存该文件的文件夹中创建一个名为uploadedfiles的文件夹.

If you use the above code you should create a folder named as uploadedfiles in the folder where you keep this file.

否则,如果需要为每个文件上传创建每个文件夹,则应进行编码.它将每次创建文件的名称作为文件夹.

Else if you need to create each folder for each file upload then you should code.. It will create the file's name as folder each time.

<!DOCTYPE html>
<html>
<body>
<form action="" 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
if (isset($_POST['submit'])) 
{
echo 'succesfully uploaded';
$structure = $_FILES["fileToUpload"]["name"];
if (!mkdir($structure, 777, true)) 
{}
$target_file = $structure.basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}
?>