简体   繁体   中英

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. 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.

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.

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.

What I would suggest is adding these files to a MYSQL table so they can be referenced and retrieved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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