从PHP上传文件到服务器

I am using the following script to upload files from a web site to the server

<?php if ((isset($_POST["enviado"])) && ($_POST["enviado"]=="form1")){
$length = 10;

$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);

$prefijo =  $randomString;


    $nombre_archivo = $_FILES['userfile']['name'];

    move_uploaded_file($_FILES['userfile']['tmp_name'],"platos/".$prefijo.$nombre_archivo);



    ?>

The selected file is uploaded to the folder platos.

On another web site, I am using the same script, the only change is the name of the folder, but the selected file is not uploaded.

This is the script used on the second site:

<?php if ((isset($_POST["enviado"])) && ($_POST["enviado"]=="form1")){
$length = 10;

$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);

$prefijo =  $randomString;


    $nombre_archivo = $_FILES['userfile']['name'];

    move_uploaded_file($_FILES['userfile']['tmp_name'],"docs/".$prefijo.$nombre_archivo);



    ?>

I am not able to detect the error...

EDITED, COMPLETE CODE HERE:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Subir Factura PFD</title>

</head>



<body>



<?php if ((isset($_POST["enviado"])) && ($_POST["enviado"]=="form1")){
$length = 10;

$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);

$prefijo =  $randomString;


    $nombre_archivo = $_FILES['userfile']['name'];

    move_uploaded_file($_FILES['userfile']['tmp_name'],"docs/".$prefijo.$nombre_archivo);



    ?>

    <script>

    opener.document.form1.pdf_factura.value="<?php echo $prefijo.$nombre_archivo;?>";

    self.close();

    </script>





    <?php 

}

else {?>

<form id="form1" name="form1" method="post" action="subirPDF.php" data-ajax="false" enctype="multipart/form-data">

  <p>

    <input name="userfile" type="file" />

  </p>

  <p>

    <input type="submit" name="button" id="button" value="Subir imagen" />

    <input type="hidden" name="enviado" value="form1" />

  </p>

</form>

<?php }?>

</body>

</html>

In case you are running this on a windows machine: I have run into kind of the same problem. move_uploaded_file() didn't work out for me because of (weird) folder permissions. I used the PHP function copy() instead of move_uploaded_file().

Hope it helps

you need to check "upload_max_filesize" and "post_max_size" paramter in your php.ini file.

These should be greater than the size of file you are trying to uploading.

; Maximum allowed size for uploaded files.
upload_max_filesize = 2M
; Must be greater than or equal to upload_max_filesize
post_max_size = 2M 

After modifying php.ini file, you need to restart your HTTP server to use new configuration.

if above parameters are ok then check the directory permission and owner.

check permissions on the directory in which you are uploading the file:

try doing:

chmod -R 777 docs

Note: The docs directory must be writable so that file can be added in this directory.