Uploading file ?
Button - 31/07/2005 18:36
PHP ver gevorderde
hallo iedereen,
ben terug begonne met scripten en heb al direct een vraagje.
ik ben namelijk begonnen met files uploaden.
hier zijn alvast the scripts:
het formulier:
<html>
<head>
<title>Administration- upload new files</title>
</head>
<body>
<h1>Upload new new files</h1>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000">
Upload this file: <input name="userfile" type="file">
<input type="submit" value=" Verzenden ">
</form>
</body>
</html>
< html>
< head>
< title> Administration- upload new files</ title>
</ head>
< body>
< h1> Upload new new files</ h1>
< form enctype= "multipart/form-data" action= "upload.php" method= "post" >
< input type= "hidden" name= "MAX_FILE_SIZE" value= "1000" >
Upload this
file : < input name
= "userfile" type
= "file" > < input type= "submit" value= " Verzenden " >
</ form>
</ body>
</ html>
de verwerkingspagina met het tonen van een preview van de pagina:
<html>
<head>
<title>Uploading</title>
</head>
<body>
<h1>Uploading file</h1>
<?
if ($userfile=="none")
{
echo "Problem: no file uploaded";
exit;
}
if($userfile_size==0)
{
echo "Problem: uploaded file is zero length";
exit;
}
if ($userfile_type != "text/plain")
{
echo "Problem: file is not plain text";
exit;
}
if (!is_uploaded_file($userfile))
{
echo "Problem: possible file upload attack";
exit;
}
$upfile = "C:\web\Chmod".$userfile_name;
if (!copy($userfile,$upfile)){
echo "Problem: Could not move file into directory";
exit;
}
echo "File uploaded succesfully<br><br>";
$fp = fopen($upfile, 'r');
$contents = fread ($fp, filesize ($upfile));
fclose ($fp);
$contents = strip_tags($contents);
$fp = fopen($upfile, "w");
fwrite($fp, $contents);
echo "Preview of uploaded file contents:<br><hr>";
echo $contents;
echo "<br><hr>";
?>
</body>
</html>
<html>
<head>
<title>Uploading</title>
</head>
<body>
<h1>Uploading file</h1>
<?
if ( $userfile == "none" )
{
echo "Problem: no file uploaded" ; }
if ( $userfile_size == 0 )
{
echo "Problem: uploaded file is zero length" ; }
if ( $userfile_type != "text/plain" )
{
echo "Problem: file is not plain text" ; }
{
echo "Problem: possible file upload attack" ; }
$upfile = "C:\web\Chmod" . $userfile_name ;
if ( ! copy ( $userfile , $upfile ) ) { echo "Problem: Could not move file into directory" ; }
echo "File uploaded succesfully<br><br>" ; $fp = fopen ( $upfile , 'r' ) ;
$fp = fopen ( $upfile , "w" ) ;
echo "Preview of uploaded file contents:<br><hr>" ;
?>
</body>
</html>
nu ik heb deze scripts van het boek: Sam PHP and MySQL Web Development.
maar als ik een bestand upload (.doc,.html,.php,...)
dan geeft hij: Problem: uploaded file is zero length
(zie script he, het is een zelfgebouwde foutmelding dus )
maar ik weet niet hoe dat komt! Jullie?
ik probeer een bestand naar mijn eigen webserver te uploaden
ps: het kan zijn dat het een stomme fout is want ik ben nogal 'noobie' op dit gebied (chmod, niet?)
op voorhand bedankt
en proficiat met de clickx number 2 !
9 antwoorden
Gesponsorde links
Thomas - 31/07/2005 18:45
Moderator
Zie dit topic en (in mijn reactie) de link naar PHP.net.
Je dient superglobals ($_POST, $_FILES etc.) te gebruiken.
Button - 31/07/2005 22:06 (laatste wijziging 31/07/2005 22:06)
PHP ver gevorderde
Zou jij of iemand anders dat dan aub willen aanpassen want ik heb verschillende dingen geprobeerd maar lukt niet...
rutgerp - 01/08/2005 09:25 (laatste wijziging 01/08/2005 09:26)
HTML interesse
Het zou zoiets moeten zijn (ik ben noob):
<html>
<head>
<title>Uploading</title>
</head>
<body>
<h1>Uploading file</h1>
<?
if (empty($_FILES['userfile']['name']))
{
echo "Problem: no file uploaded";
exit;
}
if($_FILES['userfile']['size']==0)
{
echo "Problem: uploaded file is zero length";
exit;
}
if ($FILES['userfile']['type'] != "text/plain")
{
echo "Problem: file is not plain text";
exit;
}
if (!is_uploaded_file($FILES['userfile']))
{
echo "Problem: possible file upload attack";
exit;
}
$upfile = "C:\web\Chmod".$_FILES['userfile']['name'];
if (!copy($_FILES['userfile'],$upfile)){
echo "Problem: Could not move file into directory";
exit;
}
echo "File uploaded succesfully<br><br>";
$fp = fopen($upfile.'/'.$_FILES['userfile'], 'r');
$contents = fread ($fp, filesize ($upfile.'/'.$_FILES['userfile']));
fclose ($fp);
$contents = strip_tags($contents);
$fp = fopen($upfile, "w");
fwrite($fp, $contents);
echo "Preview of uploaded file contents:<br><hr>";
echo $contents;
echo "<br><hr>";
?>
</body>
</html>
<html>
<head>
<title>Uploading</title>
</head>
<body>
<h1>Uploading file</h1>
<?
if ( empty ( $_FILES [ 'userfile' ] [ 'name' ] ) ) {
echo "Problem: no file uploaded" ; }
if ( $_FILES [ 'userfile' ] [ 'size' ] == 0 )
{
echo "Problem: uploaded file is zero length" ; }
if ( $FILES [ 'userfile' ] [ 'type' ] != "text/plain" )
{
echo "Problem: file is not plain text" ; }
{
echo "Problem: possible file upload attack" ; }
$upfile = "C:\web\Chmod" . $_FILES [ 'userfile' ] [ 'name' ] ;
if ( ! copy ( $_FILES [ 'userfile' ] , $upfile ) ) { echo "Problem: Could not move file into directory" ; }
echo "File uploaded succesfully<br><br>" ; $fp = fopen ( $upfile . '/' . $_FILES [ 'userfile' ] , 'r' ) ; $contents = fread ( $fp , filesize ( $upfile . '/' . $_FILES [ 'userfile' ] ) ) ;
$fp = fopen ( $upfile , "w" ) ;
echo "Preview of uploaded file contents:<br><hr>" ;
?>
</body>
</html>
En ik weet niet precies heo het laatste stuk gaat, maar dit ehlpt je een eindjuh op weg.
Button - 01/08/2005 10:21
PHP ver gevorderde
die $_FILES['userfile']['name'] moet $_FILES['userfile']['tmp_name'] zijn niet?
toch al bedankt ik zal het is proberen
Button - 01/08/2005 12:58 (laatste wijziging 02/08/2005 09:47)
PHP ver gevorderde
@rutgerp: die van jou lukt niet, toch bedankt
daarom heb ik deze is geprobeerd:rechts: :
<html>
<head>
<title>Uploading</title>
</head>
<body>
<h1>Uploading file</h1>
<?
if (empty($_POST['userfile']['tmp_name']))
{
echo "Problem: no file uploaded";
exit;
}
if($_POST['userfile']['size']==0)
{
echo "Problem: uploaded file is zero length";
exit;
}
if ($_POST['userfile']['type'] != "text/plain")
{
echo "Problem: file is not plain text";
exit;
}
if (!is_uploaded_file($_POST['userfile']))
{
echo "Problem: possible file upload attack";
exit;
}
$upfile = "C:\web\Chmod".$_POST['userfile']['tmp_name'];
if (!copy($_POST['userfile'],$upfile)){
echo "Problem: Could not move file into directory";
exit;
}
echo "File uploaded succesfully<br><br>";
$fp = fopen($upfile.'/'.$_POST['userfile'], 'r');
$contents = fread ($fp, filesize ($upfile.'/'.$_POST['userfile']));
fclose ($fp);
$contents = strip_tags($contents);
$fp = fopen($upfile, "w");
fwrite($fp, $contents);
echo "Preview of uploaded file contents:<br><hr>";
echo $contents;
echo "<br><hr>";
?>
</body>
</html>
<html>
<head>
<title>Uploading</title>
</head>
<body>
<h1>Uploading file</h1>
<?
if ( empty ( $_POST [ 'userfile' ] [ 'tmp_name' ] ) ) {
echo "Problem: no file uploaded" ; }
if ( $_POST [ 'userfile' ] [ 'size' ] == 0 )
{
echo "Problem: uploaded file is zero length" ; }
if ( $_POST [ 'userfile' ] [ 'type' ] != "text/plain" )
{
echo "Problem: file is not plain text" ; }
{
echo "Problem: possible file upload attack" ; }
$upfile = "C:\web\Chmod" . $_POST [ 'userfile' ] [ 'tmp_name' ] ;
if ( ! copy ( $_POST [ 'userfile' ] , $upfile ) ) { echo "Problem: Could not move file into directory" ; }
echo "File uploaded succesfully<br><br>" ; $fp = fopen ( $upfile . '/' . $_POST [ 'userfile' ] , 'r' ) ; $contents = fread ( $fp , filesize ( $upfile . '/' . $_POST [ 'userfile' ] ) ) ;
$fp = fopen ( $upfile , "w" ) ;
echo "Preview of uploaded file contents:<br><hr>" ;
?>
</body>
</html>
maar die werkt ook niet...
kan miss iemand de code is bij zichzelf ook proberen dat het miss aan mijn server liegt ofzo?
of kan je niet uploaden naar je eigen server?:!:
Thomas - 02/08/2005 11:26
Moderator
Als je PHP.net had gelezen had je kunnen weten dat je move_uploaded_file() dient te gebruiken, niet copy()...
Button - 02/08/2005 14:13 (laatste wijziging 02/08/2005 14:14)
PHP ver gevorderde
ik heb nu deze code
<?
error_reporting(E_ALL);
?>
<html>
<head>
<title>Uploading</title>
</head>
<body>
<h1>Uploading file</h1>
<?
if ($_FILES['userfile']['tmp_name']=="none")
{
echo "Problem: no file uploaded";
exit;
}
if($_FILES['userfile']['size']==0)
{
echo "Problem: uploaded file is zero length";
exit;
}
if ($_FILES['userfile']['type'] != "text/plain")
{
echo "Problem: file is not plain text";
exit;
}
if (!is_uploaded_file($_FILES['userfile']['tmp_name']))
{
echo "Problem: possible file upload attack";
exit;
}
$upfile = "C:\web\Chmod".$_FILES['userfile']['tmp_name'];
if (!move_uploaded_file($_FILES['userfile']['tmp_name'],$upfile)){
echo "Problem: Could not move file into directory";
exit;
}
echo "File uploaded succesfully<br><br>";
$fp = fopen($upfile, 'r');
$contents = fread ($fp, filesize ($upfile));
fclose ($fp);
$contents = strip_tags($contents);
$fp = fopen($upfile, "w");
fwrite($fp, $contents);
echo "Preview of uploaded file contents:<br><hr>";
echo $contents;
echo "<br><hr>";
?>
</body>
</html>
<?
?>
<html>
<head>
<title>Uploading</title>
</head>
<body>
<h1>Uploading file</h1>
<?
if ( $_FILES [ 'userfile' ] [ 'tmp_name' ] == "none" )
{
echo "Problem: no file uploaded" ; }
if ( $_FILES [ 'userfile' ] [ 'size' ] == 0 )
{
echo "Problem: uploaded file is zero length" ; }
if ( $_FILES [ 'userfile' ] [ 'type' ] != "text/plain" )
{
echo "Problem: file is not plain text" ; }
{
echo "Problem: possible file upload attack" ; }
$upfile = "C:\web\Chmod" . $_FILES [ 'userfile' ] [ 'tmp_name' ] ;
echo "Problem: Could not move file into directory" ; }
echo "File uploaded succesfully<br><br>" ; $fp = fopen ( $upfile , 'r' ) ;
$fp = fopen ( $upfile , "w" ) ;
echo "Preview of uploaded file contents:<br><hr>" ;
?>
</body>
</html>
maar krijg deze fouten:
Warning: move_uploaded_file(C:\web\ChmodC:\WINDOWS\TEMP\php3.tmp): failed to open stream: Invalid argument in c:\web\chmod\upload.php on line 38
Warning: move_uploaded_file(): Unable to move 'C:\WINDOWS\TEMP\php3.tmp' to 'C:\web\ChmodC:\WINDOWS\TEMP\ php3.tmp' in c:\web\chmod\upload.php on line 38
Problem: Could not move file into directory
-> die dingen die in het vet staan zijn volgens mij dus te veel aan... maar hoe krijg ik die uit die variable?
edit: miss kunnen jullie iets doen aan de code van dat ventje...
gamesty - 02/08/2005 14:28
Onbekend
Zet het in quote of in code.Dan heb je dat niet.
Button - 02/08/2005 16:44 (laatste wijziging 02/08/2005 17:04)
PHP ver gevorderde
over wat heb je het nou:
:\ ventje of het script
edit:
<?
error_reporting(E_ALL);
?>
<html>
<head>
<title>Uploading</title>
</head>
<body>
<h1>Uploading file</h1>
<?
if ($_FILES['userfile']['tmp_name']=="none")
{
echo "Problem: no file uploaded";
exit;
}
if($_FILES['userfile']['size']==0)
{
echo "Problem: uploaded file is zero length";
exit;
}
if ($_FILES['userfile']['type'] != "text/plain")
{
echo "Problem: file is not plain text";
exit;
}
if (!is_uploaded_file($_FILES['userfile']['tmp_name']))
{
echo "Problem: possible file upload attack";
exit;
}
$upfile = "C:\web\Chmod";
if (!move_uploaded_file($_FILES['userfile']['tmp_name'],$upfile)){
echo "Problem: Could not move file into directory";
exit;
}
echo "File uploaded succesfully<br><br>";
$fp = fopen($upfile, 'r');
$contents = fread ($fp, filesize ($upfile));
fclose ($fp);
$contents = strip_tags($contents);
$fp = fopen($upfile, "w");
fwrite($fp, $contents);
echo "Preview of uploaded file contents:<br><hr>";
echo $contents;
echo "<br><hr>";
?>
</body>
</html>
<?
?>
<html>
<head>
<title>Uploading</title>
</head>
<body>
<h1>Uploading file</h1>
<?
if ( $_FILES [ 'userfile' ] [ 'tmp_name' ] == "none" )
{
echo "Problem: no file uploaded" ; }
if ( $_FILES [ 'userfile' ] [ 'size' ] == 0 )
{
echo "Problem: uploaded file is zero length" ; }
if ( $_FILES [ 'userfile' ] [ 'type' ] != "text/plain" )
{
echo "Problem: file is not plain text" ; }
{
echo "Problem: possible file upload attack" ; }
$upfile = "C:\web\Chmod" ;
echo "Problem: Could not move file into directory" ; }
echo "File uploaded succesfully<br><br>" ; $fp = fopen ( $upfile , 'r' ) ;
$fp = fopen ( $upfile , "w" ) ;
echo "Preview of uploaded file contents:<br><hr>" ;
?>
</body>
</html>
nu ik heb de oplossing voor de vorige fout maar nu krijg ik weer deze fout: Warning: move_uploaded_file(C:\web\Chmod\): failed to open stream: Permission denied in c:\web\chmod\upload.php on line 38
Warning: move_uploaded_file(): Unable to move 'C:\WINDOWS\TEMP\php16.tmp' to 'C:\web\Chmod\' in c:\web\chmod\upload.php on line 38
Problem: Could not move file into directory
volgens mij moet ik de map rechten geven ofzo, niet? indien ja, hoe? indien nee, wat is dan het probleem...?
Gesponsorde links
Dit onderwerp is gesloten .