web-dev-qa-db-fra.com

script de téléchargement de fichier simple

J'ai écrit un script de téléchargement de fichier simple mais il me donne l'erreur d'index file1 indéfini.

<html>
    <body>
        <form method="post">
            <label for="file">Filename:</label>
            <input type="file" name="file1" id="file1" /> 
            <br />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
</html>
<?php
if(isset($_POST['submit'])) {
    if ($_FILES["file1"]["error"] > 0) {
        echo "Error: " . $_FILES["file1"]["error"] . "<br />";
    } else {
        echo "Upload: " . $_FILES["file1"]["name"] . "<br />";
        echo "Type: " . $_FILES["file1"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file1"]["size"] / 1024) . " Kb<br />";
        echo "Stored in: " . $_FILES["file1"]["tmp_name"];
    }
}
?>

Quel est le problème dans le code?

8
Ahmad

Il manque enctype="multipart/form-data" dans votre élément <form>.

13
Wesley van Opdorp

Une autre solution pour script de téléchargement de fichier php simple est ici:
(créez un yourfile.php et insérez le code ci-dessous. mettez ensuite yourfile.php sur votre site Web)

<?php
$pass = "YOUR_PASSWORD";
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1256" /></head><body>
<?php
if (!empty($_GET['action']) &&  $_GET['action'] == "logout") {session_destroy();unset ($_SESSION['pass']);}

$path_name = pathinfo($_SERVER['PHP_SELF']);
$this_script = $path_name['basename'];
if (empty($_SESSION['pass'])) {$_SESSION['pass']='';}
if (empty($_POST['pass'])) {$_POST['pass']='';}
if ( $_SESSION['pass']!== $pass) 
{
    if ($_POST['pass'] == $pass) {$_SESSION['pass'] = $pass; }
    else 
    {
        echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"><input name="pass" type="password"><input type="submit"></form>';
        exit;
    }
}
?>


<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Please choose a file: <input name="file" type="file" /><br />
<input type="submit" value="Upload" /></form>


<?php 

if (!empty($_FILES["file"]))
{
    if ($_FILES["file"]["error"] > 0)
       {echo "Error: " . $_FILES["file"]["error"] . "<br>";}
    else
       {echo "Stored file:".$_FILES["file"]["name"]."<br/>Size:".($_FILES["file"]["size"]/1024)." kB<br/>";
       move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
       }
}

    // open this directory 
    $myDirectory = opendir(".");
    // get each entry
    while($entryName = readdir($myDirectory)) {$dirArray[] = $entryName;} closedir($myDirectory);
    $indexCount = count($dirArray);
        echo "$indexCount files<br/>";
    sort($dirArray);

    echo "<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks><TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n";

        for($index=0; $index < $indexCount; $index++) 
        {
            if (substr("$dirArray[$index]", 0, 1) != ".")
            {
            echo "<TR>
            <td><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>
            <td>".filetype($dirArray[$index])."</td>
            <td>".filesize($dirArray[$index])."</td>
                </TR>";
            }
        }
    echo "</TABLE>";
    ?>
3
T.Todua

Apportez les modifications suivantes et essayez.

<form method="post" action="" enctype="multipart/form-data" >
2
Ibrahim Azhar Armar

Html

 <!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

Php

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 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;
    }
}
?>
1
arunwebber
<html>
<body>
<form action="" method="post" ectype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"></br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit']))
{
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
   if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }enter code here
}
?>
</body>
</html>
0
jafar

Apportez les modifications suivantes, tout comme le site Web suivant

$(document).ready(function(){
    $('input[teramfile-kname=teramfile_formajax]').click(function(e){
        $('.main aside[class=upload]').fadeOut('slow');
        $('.main aside[class=startupload]').delay(800).fadeIn('slow');
        e.preventDefault();
        var formData = new FormData($(this).parents('form[teramfile-fname='+$('input[teramfile-kname=teramfile_formajax]').attr('teramfile-fname')+']')[0]);
        $.ajax({
            url: $('form[teramfile-fname='+$('input[teramfile-kname=teramfile_formajax]').attr('teramfile-fname')+']').attr('action'),
            type: $('form[teramfile-fname='+$('input[teramfile-kname=teramfile_formajax]').attr('teramfile-fname')+']').attr('method'),
            xhr: function() {
                var myXhr = $.ajaxSettings.xhr();
                return myXhr;
                return false; 
            },
            success: function (data) {
                $('.main aside[class=startupload]').fadeOut('speed');  
                $('.main aside[class=resultupload]').delay(1000).html(data).fadeIn('slow');
                $('.main aside[class=newupload]').delay(1000).fadeIn();
            },
            data: formData,
            cache: false,
            contentType: false,
            processData: false
        });
        return false;
    });
});

http://www.teramfile.com/filedetails/e4fd5b2ec2/

0
Join boone