简体   繁体   English

无法将文件上传到 php 中的 web 站点服务器

[英]Cant upload file to web site server in php

Im using this code我正在使用此代码

if(isset($_POST['upload'])) {
    $file = $_FILES['file'];

    $file_name = $_FILES['file']['name'];
    $file_type = $_FILES['file']['type'];
    $file_size = $_FILES['file']['size'];
    $file_tem_loc = $_FILES['file']['tmp_name'];
    $file_store = "pdf/".$file_name;
    move_uploaded_file($file_tem_loc, $file_store);
}

to upload a file.It works on my computer.上传文件。它在我的电脑上工作。 I can copy the pdf file to my destination folder but on the website server this code doesnt work i dont know why.我可以将 pdf 文件复制到我的目标文件夹,但在网站服务器上,此代码不起作用,我不知道为什么。 Btw sry about my bad language (:顺便说一句,我的语言不好(:

Here's a script I developed a while back which works well.这是我不久前开发的一个脚本,效果很好。

It checks for file size: any images over 1Mb will be rejected, you can change this though if you want to upload bigger or smaller files.它检查文件大小:任何超过 1Mb 的图像都将被拒绝,但如果您想上传更大或更小的文件,您可以更改此设置。

The script also checks for file type just in case anyone tries to upload a potentially malicious file.该脚本还会检查文件类型,以防万一有人尝试上传潜在的恶意文件。 $extension used to only allow images but I added pdf files for you but be careful, pdf's can easily be infected while images not so much so I hope your application/connection is secured properly with a protected login. $extension 过去只允许使用图像,但我为您添加了 pdf 文件,但要小心,pdf 很容易被感染,而图像则不那么容易感染,所以我希望您的应用程序/连接通过受保护的登录得到正确保护。

Lastly the script renames the file based on time so avoiding any name conflicts which would otherwise eventually occur after multiple files have been uploaded.最后,脚本会根据时间重命名文件,以避免在上传多个文件后最终发生的任何名称冲突。

<?php
// File settings
$target_dir = "pdf/"; // Make sure you get this folder location correct, currently it is http://www.yourwebsite.com/pdf
$UploadOk = true;
$extension = array("jpeg","jpg","png","gif","pdf");
$bytes = 1024; // Change to 2048 for 2Mb or 4096 for 4Mb etc.
$KB = 1024; // Change to 2048 for 2Mb or 4096 for 4Mb etc.
$totalBytes = $bytes * $KB;

// Grab file
if (isset($_FILES['upload']['name'])) {
$total_files = count($_FILES['upload']['name']);

// Check file size   
if($_FILES["upload"]["size"] > $totalBytes) {
$UploadOk = false;
echo "<script>alert('Error: File must be less than 1mb!')</script>"; // Change file size warning for larger files
echo "<script>window.history.back();</script>";
}

// Check file extension
$ext = strtolower(pathinfo($_FILES["upload"]["name"], PATHINFO_EXTENSION));
if(in_array($ext, $extension) == false) {
$UploadOk = false;
echo "<script>alert('Error: Images only!')</script>";
echo "<script>window.history.back();</script>";
}

// If checks passed, rename file and upload to directory
if ($UploadOk ==  true) {
$time = preg_replace('/(0)\.(\d+) (\d+)/', '$3$1$2', microtime());
$dot = ".";
$new_filename = $time.$dot.$ext;
move_uploaded_file($_FILES['upload']['tmp_name'], $target_dir . $new_filename);
}
}
?>

If you still can't upload the file despite the directory location being correct, as mentioned check the owner permissions for the folder in question.如果尽管目录位置正确,您仍然无法上传文件,如上所述,请检查相关文件夹的所有者权限。 You can do this by navigating to your website folder using an FTP program or through cPanel and right clicking the folder in question to check the permissions, 755 is okay and should allow uploads.您可以通过使用 FTP 程序或通过 cPanel 导航到您的网站文件夹来执行此操作,然后右键单击有问题的文件夹以检查权限,755 没问题,应该允许上传。

What I would suggest is adding these files to a MYSQL table so they can be referenced and retrieved.我建议将这些文件添加到 MYSQL 表中,以便可以引用和检索它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM