fileupload
Michael
- php
ey guys,
can anybody please tell me how to get a working file upload with php?
I'm a lil confused right now. My script works as far as the uplod function is concerned. It makes a new file php... in the tmp directory and the file gets bigger and bigger. But as soon as the script should move/copy/renam the uploaded file, the file is gone.
Here's my script so far:
<?
$uploaddir = '/opt/uploaded/';
$uploadfile = $uploaddir. $HTTP_POST_FILES['userfile']['name'];
print "<pre>";
if (is_uploaded_file($HTTP_POST_FILES['userfile'])) {
rename($_FILES['userfile']['tmp_name'], "/opt/uploaded/123.mpg");
} else {
echo "Mögliche Dateiupload-Attacke: Dateiname '$HTTP_POST_FILES[userfile]'.";
}
print "</pre>";
?>
<!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>Untitled Document</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input name="userfile" type="file" /><input type="text" name="filename" /><input type="submit" />
</form>
<? phpinfo();?>
</body>
</html>
or
<?
function upload_file($field = '', $dirPath = '', $maxSize = 100000, $allowed = array())
{
foreach ($_FILES[$field] as $key => $val)
$$key = $val;
if ((!is_uploaded_file($tmp_name)) || ($error != 0) || ($size == 0) || ($size > $maxSize))
return false; // file failed basic validation checks
if ((is_array($allowed)) && (!empty($allowed)))
if (!in_array($type, $allowed))
return false; // file is not an allowed type
do $path = $dirPath . DIRECTORY_SEPARATOR . rand(1, 9999) . strtolower(basename($name));
while (file_exists($path));
if (move_uploaded_file($tmp_name, "/opt/uploaded/123.mp3"))
return $path;
return false;
}
?>
DEMO:
<?php
if (array_key_exists('submit', $_POST)) // form has been submitted
{
if ($filepath = upload_file('music_upload', 'music_files', 700000, array('application/octet-stream','audio/wav')))
echo 'File uploaded to ' . $filepath;
else
echo 'An error occurred uploading the file... please try again.';
}
echo '
<form method="post" action="' .$_SERVER['PHP_SELF']. '" enctype="multipart/form-data">
<input type="file" name="music_upload" id="music_upload" />
<input type="submit" name="submit" value="submit" />
</form>
';
print_r($_FILES); // for debug purposes
?>
The rights of my tmp are 777 and chown nobody.
Thanks in advance
Hallo,
can anybody please tell me how to get a working file upload with php?
the current documentation can :-) Why didn't you read the f*** manual?
Your code is very ugly, uses deprecated features and there's no attempt to get any information about possible errors.
Warum schreibst Du Englisch in einem deutschsprachigen Forum? Es sieht sehr danach aus, dass Deine Muttersprache Deutsch ist ...
Grüße
Vinzenz
Lieber Vinzenz,
Warum schreibst Du Englisch in einem deutschsprachigen Forum?
gute Frage!
Es sieht sehr danach aus, dass Deine Muttersprache Deutsch ist ...
Woraus schließt Du das? Etwa aus diesem letzten Satz?
The rights of my tmp are 777[...]
Liebe Grüße,
Felix Riesterer.
Hi, ich habe jetzt exakt den Code aus der Anleitung genommen aber es gibt immer noch den selben Fehler. Er lädt die Datei ins tmp hoch, löscht Sie aber bevor er durch move_uploaded_file diese ins verzeichnis legt.
Possible file upload attack!
Here is some more debugging info:Array
(
[userfile] => Array
(
[name] => myRouter.clr
[type] => application/octet-stream
[tmp_name] => /opt/uploaded/phpGdtvqr
[error] => 0
[size] => 410
)
)
<!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>Untitled Document</title>
</head>
<body>
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="1234.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = '/opt/uploaded/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
</body>
</html>
Hi,
Er lädt die Datei ins tmp hoch, löscht Sie aber bevor er durch move_uploaded_file diese ins verzeichnis legt.
Wie hast du diese Annahme ueberprueft?
$uploaddir = '/opt/uploaded/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
Ich vermute ja, dass das hier entweder mangels Rechten, oder wegen unsinniger Variableninhalte schief geht.
Dein Debugging an dieser Stelle hat was ergeben?
MfG ChrisB
Ich habe mich mit der Konsole ins tmp Verzeichnis geschaut und per dir mir die Dateien angesehen. Es wird auch eine php... erstellt, die immer größer wird und plötzlich weg ist. Die Rechte des Verzeichnises sind 777
Hoffe ihr habt vllt noch eine Idee.
Viele Grüße und Danke im Voraus
Hi,
Er lädt die Datei ins tmp hoch, löscht Sie aber bevor er durch move_uploaded_file diese ins verzeichnis legt.
Wie hast du diese Annahme ueberprueft?
$uploaddir = '/opt/uploaded/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {Ich vermute ja, dass das hier entweder mangels Rechten, oder wegen unsinniger Variableninhalte schief geht.
Dein Debugging an dieser Stelle hat was ergeben?MfG ChrisB
Hi,
Ich habe mich mit der Konsole ins tmp Verzeichnis geschaut und per dir mir die Dateien angesehen. Es wird auch eine php... erstellt, die immer größer wird und plötzlich weg ist.
Natuerlich, zum Scriptende werden ins Tep-Verzeichnis hochgeladene Dateien von PHP entsorgt, sofern man sie nicht woandershin verschoben hat.
Hoffe ihr habt vllt noch eine Idee.
Ich fragte dich bereits, was dein Debugging ergeben hat.
MfG ChrisB
Hi, das war das Ergebnis (falls man das als debugging bezeichnen kann):
Array ( [userfile] => Array ( [name] => putty.exe [type] => application/octet-stream [tmp_name] => /opt/uploaded/phpGdvF1r [error] => 0 [size] => 454656 ) )
Wenn du noch eine andere Debugging-Methode kennst bitte posten wäre super.
Viele Grüße
Hi,
Ich habe mich mit der Konsole ins tmp Verzeichnis geschaut und per dir mir die Dateien angesehen. Es wird auch eine php... erstellt, die immer größer wird und plötzlich weg ist.
Natuerlich, zum Scriptende werden ins Tep-Verzeichnis hochgeladene Dateien von PHP entsorgt, sofern man sie nicht woandershin verschoben hat.
Hoffe ihr habt vllt noch eine Idee.
Ich fragte dich bereits, was dein Debugging ergeben hat.
MfG ChrisB
Hallo,
Hi, das war das Ergebnis (falls man das als debugging bezeichnen kann):
Array ( [userfile] => Array ( [name] => putty.exe [type] => application/octet-stream [tmp_name] => /opt/uploaded/phpGdvF1r [error] => 0 [size] => 454656 ) )
Wenn du noch eine andere Debugging-Methode kennst bitte posten wäre super.
Standardmethoden:
error_reporting(E_ALL);
und die Einstellung von
prüfen. Wie die Doku Dir sagt, gibt es eine Warnung, wenn move_uploaded_file() eine Datei nicht verschieben kann. Genau diese Warnung sollte Dir Auskunft geben, warum Deine Datei nicht verschoben werden kann.
Freundliche Grüße
Vinzenz
Vielen Dank, hab es schon: habe das open_basedir auf /srv/www/vhosts gelegt und die upload_tmp_dir auf /srv/www/vhosts/tmp. Dann funkt es perfekt. Vielen Dank
Hallo,
Hi, das war das Ergebnis (falls man das als debugging bezeichnen kann):
Array ( [userfile] => Array ( [name] => putty.exe [type] => application/octet-stream [tmp_name] => /opt/uploaded/phpGdvF1r [error] => 0 [size] => 454656 ) )
Wenn du noch eine andere Debugging-Methode kennst bitte posten wäre super.
Standardmethoden:
error_reporting(E_ALL);
und die Einstellung von
prüfen. Wie die Doku Dir sagt, gibt es eine Warnung, wenn move_uploaded_file() eine Datei nicht verschieben kann. Genau diese Warnung sollte Dir Auskunft geben, warum Deine Datei nicht verschoben werden kann.
Freundliche Grüße
Vinzenz
Hey, vielen Dank für euere Hilfe, bin heute ein rießen Stück weiter gekommen. Der Upload von Putty hat perfekt geklappt. Hab in der php.ini mein upload_tmp auf /srv/www/vhosts/domain.de/httpdocs/cms geändert und es geht jetzt. Jetzt nur noch meine Frage, weil ich mehrere Domains da drauf laufen habe. Wie schaff ich es, dass ich /srv/www/vhosts/tmp als tmp nehmen kann ohne dass es außerhalb meines open_basedirs liegt und ohne dass ich die php ini jedes mal änern muss?
Vielen Dank und schöne Grüße
ey guys,
can anybody please tell me how to get a working file upload with php?
I'm a lil confused right now. My script works as far as the uplod function is concerned. It makes a new file php... in the tmp directory and the file gets bigger and bigger. But as soon as the script should move/copy/renam the uploaded file, the file is gone.Here's my script so far:
<?
$uploaddir = '/opt/uploaded/';
$uploadfile = $uploaddir. $HTTP_POST_FILES['userfile']['name'];print "<pre>";
if (is_uploaded_file($HTTP_POST_FILES['userfile'])) {
rename($_FILES['userfile']['tmp_name'], "/opt/uploaded/123.mpg");
} else {
echo "Mögliche Dateiupload-Attacke: Dateiname '$HTTP_POST_FILES[userfile]'.";
}
print "</pre>";?>
<!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>Untitled Document</title>
</head><body>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input name="userfile" type="file" /><input type="text" name="filename" /><input type="submit" />
</form>
<? phpinfo();?>
</body>
</html>or
<?
@function upload_file
@param $field string the name of the file upload form field
@param $dirPath string the relative path to which to store the file (no trailing slash)
@param $maxSize int the maximum size of the file
@param $allowed array an array containing all the "allowed" file mime-types
@return mixed the files' stored path on success, false on failure.
function upload_file($field = '', $dirPath = '', $maxSize = 100000, $allowed = array())
{
foreach ($_FILES[$field] as $key => $val)
$$key = $val;if ((!is_uploaded_file($tmp_name)) || ($error != 0) || ($size == 0) || ($size > $maxSize))
return false; // file failed basic validation checksif ((is_array($allowed)) && (!empty($allowed)))
if (!in_array($type, $allowed))
return false; // file is not an allowed typedo $path = $dirPath . DIRECTORY_SEPARATOR . rand(1, 9999) . strtolower(basename($name));
while (file_exists($path));if (move_uploaded_file($tmp_name, "/opt/uploaded/123.mp3"))
return $path;return false;
}
?>DEMO:
<?phpif (array_key_exists('submit', $_POST)) // form has been submitted
{
if ($filepath = upload_file('music_upload', 'music_files', 700000, array('application/octet-stream','audio/wav')))
echo 'File uploaded to ' . $filepath;
else
echo 'An error occurred uploading the file... please try again.';
}
echo '
<form method="post" action="' .$_SERVER['PHP_SELF']. '" enctype="multipart/form-data">
<input type="file" name="music_upload" id="music_upload" />
<input type="submit" name="submit" value="submit" />
</form>
';print_r($_FILES); // for debug purposes
?>
The rights of my tmp are 777 and chown nobody.
Thanks in advance