Uploading a file in PHP -
i'm trying upload file using php.
my folder structure following:
i have directory called schemas
in root htdocs folder. inside folder, want create new folder every user; in folder i'll storing user-specific files.
for now, code incomplete (doesn't check whether or not folder/file exists , might error on this), i'm trying basics work.
<?php if ($_files ["bestand"] ["error"] > 0) { //for error messages: see http://php.net/manual/en/features.fileupload.errors.php switch ($_files ["bestand"] ["error"]) { case 1 : $msg = "u mag maximaal 2mb opladen."; break; default : $msg = "sorry, uw upload kon niet worden verwerkt."; } } else { //check mime type - http://php.net/manual/en/function.finfo-open.php $allowedtypes = array ("application/pdf" ); $filename = $_files ["bestand"] ["tmp_name"]; $finfo = new finfo ( fileinfo_mime_type ); $fileinfo = $finfo->file ( $filename ); if (in_array ( $fileinfo, $allowedtypes )) { //move uploaded file $dir = "schemas"; chdir("schemas"); $user_folder = str_replace(" ", "", $_post['schema']); mkdir( $user_folder, 0777); // echo getcwd(); // closedir($open); $folder = "/schemas/" . $user_folder . "/" . $_files ["bestand"] ["name"]; echo $folder; echo $user_folder; if (move_uploaded_file ( $_files ["bestand"] ["tmp_name"], $folder )) { $msg = "uw schema succesvol geupload!"; } else { $msg = "upload mislukt."; } } else { $msg = "u kan enkel een pdf uploaden."; } } echo $msg . "<br />"; ?>
when try this, following warnings:
warning: move_uploaded_file(/schemas/loesp/loopschema-0-tot-5-kilometer.pdf) [function.move-uploaded-file]: failed open stream: no such file or directory in /applications/mamp/htdocs/sportjefit2/uploadenfile.php on line 30 warning: move_uploaded_file() [function.move-uploaded-file]: unable move '/applications/mamp/tmp/php/phpmsxxsu' '/schemas/loesp/loopschema-0-tot-5-kilometer.pdf' in /applications/mamp/htdocs/sportjefit2/uploadenfile.php on line 30
the script does create folder (in case loesp
) not seem upload or move actual file. i'm stumped.
the problem you're using absolute path, starts in root of entire machine. should use relative path, , should start current working directory.
you inside schemas
folder (after call chdir), $folder should set this:
$folder = $user_folder . "/" . $_files ["bestand"] ["name"];
Comments
Post a Comment