无法通过PHP上传服务器上的图像

时间:2013-03-12 19:14:56

标签: php

所以我在php中使用这个代码来获取表单的结果:

<?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
    $fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

    $path = $_SERVER['DOCUMENT_ROOT'] . "/uploads/";
    //$path = "uploads/";
    $path = $path . basename( $_FILES['userfile']['name'] );

    is_uploaded_file( $_FILES['userfile']['tmp_name'] ) or error( 'not an HTTP upload' ); 

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $path ) )
    {
        echo "The file ". basename( $_FILES['userfile']['name'] ) . " has been uploaded, and your information has been added to the directory"; 
    } 
    else
    {
        echo "Sorry, there was a problem uploading your file -->" . $path;
    }


    } ?>

我遇到的问题是代码经过is_uploaded_file但在执行move_uploaded_file时失败

这段代码是我在许多php论坛和教程中找到的代码。

任何人都知道为什么它不会在我的服务器上传。

编辑:

这是带有html代码的完整.php文件:

<!DOCTYPE html>
<html>
<head>
    <title>Montréal Accessible</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="vendors/bootstrap/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css">
    <link rel="stylesheet" type="text/css" href="stylesheets/home.css"/>
    <link rel="stylesheet" type="text/css" href="stylesheets/grid.css">
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD9wqmeX_YlaRLZYrnNDOkHcXvlXa9cATo&sensor=false"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
    <script type="text/javascript" src="scripts/home.js"></script>

    <?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
    {
        $fileName = $_FILES['userfile']['name'];
        $tmpName  = $_FILES['userfile']['tmp_name'];
        $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];

        $path = $_SERVER['DOCUMENT_ROOT'] . "/uploads/";
        //$path = "uploads/";
        $path = $path . basename( $_FILES['userfile']['name'] );

        is_uploaded_file( $_FILES['userfile']['tmp_name'] ) or error( 'not an HTTP upload' ); 

        if (move_uploaded_file($_FILES['userfile']['tmp_name'], $path ) )
        {
            echo "The file ". basename( $_FILES['userfile']['name'] ) . " has been uploaded, and your information has been added to the directory"; 
        } 
        else
        {
            echo "Sorry, there was a problem uploading your file -->" . $path;
        }

        $myvars = '';
        $ch = curl_init( $url );
        curl_setopt( $ch, CURLOPT_POST, 1);
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

        $upImgResponse = curl_exec( $ch );*/
    } ?>

    <script type="text/javascript">
        window.onload = function()
        {
            loginForm = document.forms[0];
            loginForm.onsubmit=function()
            {
                var name = prompt ("Enter your name","")

                if( loginForm[0].value == '' )
                {
                    alert('Please enter a username');
                    return false;
                }
                else 
                {
                    loginForm.action='../Symfony/web/app.php/login/' + name + '/' + ...;
                    return true;
                }
            }
        }
    </script>

</head>
<body>
    <div id="container">
        <div id="headerBar">
            <div id="homeLogoColumn">
                <div id="homeLogo">Montréal Accessible</div>
            </div>
            <div id="searchBarColumn">
                <div id="searchBar" class="input-append">
                  <input id="appendedInputButton" type="text">
                  <a id="searchButton" class="btn btn-primary" href="#"><i class="icon-search icon-white"></i></a>
                </div>
            </div>
            <div id="anchorsColumn">
                        <a class="lastAnchor" href="#">À propos</a>
                        <a href="#">Contact</a>
                        <a href="#">Aide</a>
                        <!--<form name="input" action="" method="post">
                        <a href="#">Connexion</a><input type="submit" value="Connect">
                        </form> -->

            </div>
        </div>


        <form method="post" enctype="multipart/form-data">
        <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
        <tr>
        <td width="246">
        <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
        <input name="userfile" type="file" id="userfile">
        </td>
        <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
        </tr>
        </table>
        </form>

        <div id="mainContent">
                <div id="paramsAndResultsColumn">
                    <div id="paramsAndResultsTabs">
                      <ul>
                        <li><a href="#paramsTab"><span>Critères</span></a></li>
                        <li><a href="#resultsTab"><span>Résultats</span></a></li>
                      </ul>
                      <div id="paramsTab"></div>
                      <div id="resultsTab"></div>
                    </div>
                </div>
                <div id="mapCanvasColumn">
                    <div id="mapCanvas"></div>
                </div>
        </div>
    </div>
</body>
</html>

编辑2:

显示的错误是“抱歉,上传时出现问题....”意味着它会通过其他内容...

2 个答案:

答案 0 :(得分:0)

使用这个`      0)     {         $ fileName = $ _FILES ['userfile'] ['name'];         $ tmpName = $ _FILES ['userfile'] ['tmp_name'];         $ fileSize = $ _FILES ['userfile'] ['size'];         $ fileType = $ _FILES ['userfile'] ['type'];         echo $ tmpName;

    $path = "uploads/";
    $path = $path . basename( $_FILES['userfile']['name'] );

    is_uploaded_file( $_FILES['userfile']['tmp_name'] ) or error( 'not an HTTP upload' ); 

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $path ) )
    {
        echo "The file ". basename( $_FILES['userfile']['name'] ) . " has been uploaded, and your information has been added to the directory"; 
    } 
    else
    {
        echo "Sorry, there was a problem uploading your file -->" . $path;
    }

    /*$url="http://rapliq.lerb.polymtl.ca/Symfony/web/app.php/upimg/" . $fileName . "/" . $path . "/";

    $myvars = '';
    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_POST, 1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt( $ch, CURLOPT_HEADER, 0);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

    $upImgResponse = curl_exec( $ch );*/
} ?>`

这个问题是洗澡你不必给主根。另外,请确保该文件夹可访问。

答案 1 :(得分:0)

感谢您的帮助......问题是权限问题。我使用chmod 755而不是chmod 777

设置了文件夹的权限
相关问题