Home > Backend Development > PHP Tutorial > PHP code for uploading files without refreshing

PHP code for uploading files without refreshing

WBOY
Release: 2016-07-25 08:52:06
Original
990 people have browsed it
  1. Upload files without refreshing
  • < /html>
  • Copy code

    File: upload.php

    1. sleep(2);

    2. $fileTypes = array('jpg','png','gif','bmp ');

    3. $result = null;
    4. $uploadDir = './upfiles';
    5. $maxSize = 1 * pow(2,20);

    6. if ($_SERVER['REQUEST_METHOD' ] == 'POST' && isset($_POST['sub'])) {

    7. $myfile = $_FILES['myfile'];
    8. $myfileType = substr($myfile['name'], strrpos($myfile[ 'name'], ".") + 1);

    9. if ($myfile['size'] > $maxSize) {

    10. $result = 1;
    11. } else if (! in_array($myfileType, $fileTypes)) {
    12. $result = 2;
    13. } elseif (is_uploaded_file($myfile['tmp_name'])) {
    14. $toFile = $uploadDir . '/' . $myfile['name'] ;
    15. if (@move_uploaded_file($myfile['tmp_name'], $toFile)) {
    16. $result = 0;
    17. } else {
    18. $result = -1;
    19. }
    20. } else {
    21. $result = 1;
    22. }
    23. }
    24. ?>
    25. < /p>
    Copy code


    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template