PHP:无法从管理员上传文件

时间:2018-08-25 17:09:18

标签: php file-upload

我需要从管理区域上传3个不同的文件,这些文件将分别放在不同的目录中。 2是文本文件,1是图像文件。

我想启发w3schools的PHP部分示例,即:

原始

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

我的修改后的版本有3个文件,是:

<?php
if ($_POST["rtestoita"]!="") {

$target_dirIT = "/filecomunicati/";
$target_fileIT = $target_dirIT . basename($_FILES["filetesto"]);

$uploadOkIT = 1;}

if ($_POST["rtestoing"]!="") {
$target_dirEN = "/filecomunicati/";
$target_fileEN = $target_dirEN . basename($_FILES["filetestoing"]);

$uploadOkEN = 1;}

if ($_POST["rfoto"]!=""){
$target_dirIMG = "/imgcomunicati/";
$target_fileIMG = $target_dirIMG . basename($_FILES["imagefile"]);

$uploadOkIMG = 1;
$imageFileType = strtolower(pathinfo($target_fileIMG,PATHINFO_EXTENSION));
}
if(isset($_POST["Go"])){
$servername = "xxx.xxx.xxx.xxx";
$username = "goofy";
$password = "mypassword";
$dbname = "mydb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Check if image file is a actual image or fake image
if ($_POST["imagefile"]!=""){
    $check = getimagesize($_FILES["imagefile"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOkIMG = 0;
}
// Check file size
if ($_FILES["imagefile"]["size"] > 2000000) {
    echo "Sorry, your file is too large.";
    $uploadOkIMG = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOkIMG = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOkIMG == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["imagefile"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["imagefile"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

if ($_POST["testofile"]!=""){
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOkIT = 0;
}
}
// Check file size
if ($_FILES["testofile"]["size"] > 2000000) {
    echo "Sorry, your file is too large.";
    $uploadOkIT = 0;
}   
// Check if $uploadOk is set to 0 by an error
if ($uploadOkIT == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["testofile"]["tmp_" & $_POST["rtestoita"]], $target_file)) {
        echo "The file ". basename( $_FILES["testofile"] & $_POST["rtestoita"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

if ($_POST["testofileing"]!=""){
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOkEN = 0;
}
}
// Check file size
if ($_FILES["testofileing"]["size"] > 2000000) {
    echo "Sorry, your file is too large.";
    $uploadOkEN = 0;
}   
// Check if $uploadOk is set to 0 by an error
if ($uploadOkEN == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["testofileing"]["tmp_" & $_POST["rtestoing"]], $target_file)) {
        echo "The file ". basename( $_FILES["testofileing"] & $_POST["rtestoing"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}



if (empty($_POST["rfoto"])) 
            {$sql = "INSERT INTO comunicati (id, idoperatore, operatore, data, titolo, argomento, titoloing, argomentoing, fileita, fileing, primapagina , img, foto, pathtxt, pathimg, tipotesto, tipoimg, note)
VALUES ('".$_POST["NULL"]."','".$_POST["idoperatore"]."','".$_POST["operatore"]."','".$_POST["data"]."','".$_POST["titolo"]."','".$_POST["argomento"]."', '".$_POST["titoloing"]."','".$_POST["argomentoing"]."','".$_POST["fileita"]."','".$_POST["fileing"]."','".$_POST["primapagina"]."','".$_POST["logopreso"]."', '".$_POST["logopreso"]."','".$_POST["pathtxt"]."','".$_POST["pathimg"]."','".$_POST["tipotesto"]."','".$_POST["tipoimg"]."','".$_POST["note"]."')";
} else
    {$sql = "INSERT INTO comunicati (id, idoperatore, operatore, data, titolo, argomento, titoloing, argomentoing, fileita, fileing, primapagina , img, foto, pathtxt, pathimg, tipotesto, tipoimg, note)
VALUES ('".$_POST["NULL"]."','".$_POST["idoperatore"]."','".$_POST["operatore"]."','".$_POST["data"]."','".$_POST["titolo"]."','".$_POST["argomento"]."', '".$_POST["titoloing"]."','".$_POST["argomentoing"]."','".$_POST["fileita"]."','".$_POST["fileing"]."','".$_POST["primapagina"]."','".$_POST["rfoto"]."', '".$_POST["rfoto"]."','".$_POST["pathtxt"]."','".$_POST["pathimg"]."','".$_POST["tipotesto"]."','".$_POST["tipoimg"]."','".$_POST["note"]."')";
}

但是不会上传。还有一个问题是,在输入文本中仅应写入文本文件名的地方,我有c:\fakepath\myfile.ext,在文件名的DB字段中,我得到了c:fakepatmyfle.ext

这是我的HTML。我必须在每个相应的文本框中的3个输入文件中都显示相同的文本(但只能是简单的文本,没有路径),因此我正在考虑将此脚本放在</head>

之前
    <script>
document.getElementById( 'rfoto' ).value = document.getElementById( 'imagefile' ).files[ 0 ].name;
</script>

    <script>
function copiaIT(){   $("#rtestoita").val($("#testofile").val());}
function copiaEN(){   $("#rtestoing").val($("#testofileing").val());}
function copiaIMG(){   $("#rfoto").val($("#imagefile").val());

}
</script> 

我的表单是:

<div class="container">

  <h4>INSERIMENTO COMUNICATI</h4> 


 <div class="container">
<form class="form-horizontal" action=<?php echo $_SERVER['PHP_SELF']; ?> method="post" enctype="multipart/form-data">
            <div class="form-group">
   <label for="id">id:</label>
      <input type="text" class="form-control" id="id" placeholder="ID" name="id" size="5">
    </div>

            <div class="form-group">
   <label for="idoperatore">id Operatore:</label>
      <input type="text" class="form-control" id="idoperatore" placeholder="id Operatore" name="idoperatore" value="<?Php echo($idoperatore) ?>" Maxlength ="5"/>
    </div>
                    <div class="form-group">
   <label for="operatore">Operatore:</label>
      <input type="text" class="form-control" id="operatore" placeholder="Operatore" name="operatore" value="<?Php echo($operatore) ?>" maxlength="20"/>
    </div>
<div class="row">
   <div class="col-sm-2 col-lg-2">
    <div class="form-group date">

   <label for="data">Data:</label>
                <div class="input-group">   
      <input type="text" class="form-control"  id="datepicker" placeholder="Data" name="data" value="<?php echo($data); ?>" readonly="readonly">
      <span class="input-group-btn">
        <button class="btn btn-default" type="button"><span class="glyphicon glyphicon-calendar"></span></button>


                    </div></div>
</div></div>

            <div class="form-group">
            <label for="testofile">File Testo Italiano:</label>
            <input type="file"id="testofile" name="testofile" onchange="javascript:copiaIT()">

                    </div>
                    <div class="form-group">
                    <input name="rtestoita" class="form-control"  id="rtestoita" type="text"  value="<?Php echo($rtestoita) ?>" size="25"/>             
<input  type="hidden"id="pathtxt" class="form-control" name="pathtxt"  value="filecomunicati/" >
<input  type="hidden"id="tipotesto" class="form-control" name="tipotesto"  value="text/plain" >
                    </div>
<div class="form-group">
<label for="testofileing">File Testo Inglese:</label>
    <input name="testofileing" type="file" id="testofileing" onchange="javascript:copiaEN()"> 

            </div>

            <div class="form-group">
                    <input name="rtestoing" class="form-control"  id="rtestoing" type="text"  value="<?Php echo($rtestoing) ?>" size="25" readonly="readonly"/>             
</div>
            <div class="form-group">



    <input type="file" name="imagefile" id="imagefile" onchange="javascript:copiaIMG()">


    <input name="rfoto" id="rfoto" type="text" value="<?Php echo($rfoto) ?>" size="25" readonly/>
    NB: SE NON VERRA' SELEZIONATO ALCUN FILE DA CARICARE, VERRA' INSERITO AUTOMATICAMENTE IL FILE "LOGOPRESO.JPG".
    <input  type="hidden"id="pathimg" class="form-control" name="pathimg"  value="imgcomunicati/" >
<input  type="hidden"id="tipoimg" class="form-control" name="tipoimg"  value="image/jpeg" >
<input  type="hidden"id="logopreso" class="form-control" name="logopreso"  value="logopreso.JPG" >
    </div>

    <div class="form-group">
            <label for="visualizza">Uploaded Image:</label>
            <span id="uploaded_image"></span>

        </div>


                        <div class="form-group">
   <label for="titolo">Titolo Italiano:</label>
      <input type="text" class="form-control" id="titolo" placeholder="Titolo" name="titolo" value="<?Php echo($titolo) ?>" maxlength="150"/>
            </div>

            <div class="form-group">
   <label for="argomento">Argomento Italiano:</label>
      <input type="text" class="form-control" id="argomento" placeholder="Argomento" name="argomento" value="<?Php echo($argomento) ?>" maxlength="150"/>
            </div>

<div class="form-group">
   <label for="titoloing">Titolo Inglese:</label>
      <input type="text" class="form-control" id="titoloing" placeholder="Titolo inglese" name="titoloing" value="<?Php echo($titoloing) ?>" maxlength="150"/>
            </div>

            <div class="form-group">
   <label for="argomentoing">Argomento Inglese:</label>
      <input type="text" class="form-control" id="argomentoing" placeholder="Argomento inglese" name="argomentoing" value="<?Php echo($argomentoing) ?>" maxlength="150"/>
            </div>

        <div class="form-group">
   <label for="note">Note:</label>
      <input type="text" class="form-control" id="note" placeholder="Note" name="note" value="<?Php echo($note) ?>" maxlength="150"/>
    </div>              

    <div class="form-group">
    <label for="Prima Pagina">Prima pagina:</label>
   <input name="primapagina" value="1" type="checkbox" id="checkbox" <?Php echo($checked) ?> > 

   </div>

            <table align="center">
    <button name="Go" value="Submit" type="submit" class="btn btn-success btn-block">Invia</button> 
    <br>
    </table>

我想就是全部...我在这里放了很多代码...请不要讨厌我:)我只是想在一篇文章中解释这种情况。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我通过使用fakepath修改了论坛上在此处找到的脚本来解决了这个问题。无论如何,这就是我所做的:

    <script>
function theimage(){
 var filenameIMG = document.getElementById("imagefile").files[0].name; 
 document.getElementById('rfoto').value = filenameIMG;
 }

 function thetxtITA() {
 var filenameIT = document.getElementById("testofile").files[0].name; 
 document.getElementById('rtestoita').value = filenameIT;
 }

 function thetxtEN() {
 var filenameEN = document.getElementById("testofileing").files[0].name; 
 document.getElementById('rtestoing').value = filenameEN;
}
</script>

在表单中,我只是通过OnChange调用了每个函数。

现在也解决了上传问题,这要归功于论坛上的另一篇文章:PHP upload multiple files with different input field names