开发者

PHP upload file jquery

Ive got contact form with PHP and jquery. Everything works fine except the upload file. its not uploaded to the folder. if i put the PHP code in the same page as html page, the upload file works fine?

HTML:

<form id="vacancy" method="post" enctype="multipart/form-data">
<label>Full name</label><input type="text" id="fullname">
<label>Please upload your cv</label><input name="uploadcv" type="file" id="uploadcv">
<input type="submit" value="Submit Application" id="submit">
</form>

jQuery:

$("#vacancy").submit(function() {           
   $.post ( "include/vacancy.php", {
       fullname: $("#fullname").val(), 
       uploadcv: $("#uploadcv").val()},
function(data){ });

PHP:

  $name_of_uploaded_file =  basename($_FILES['uploadcv']开发者_如何学运维['name']);
  $type_of_uploaded_file = 
        substr($name_of_uploaded_file, 
        strrpos($name_of_uploaded_file, '.') + 1);

    $size_of_uploaded_file = $_FILES["uploadcv"]["size"]/1024;//size in KBs

    $max_allowed_file_size = 100; // size in KB 
    $allowed_extensions = array("jpg", "jpeg", "gif", "bmp", 'doc', 'docx', 'pdf');
    $upload_folder = 'cv/'; //<-- this folder must be writeable by the script
    $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
    $tmp_path = $_FILES["uploadcv"]["tmp_name"];
    if(is_uploaded_file($tmp_path)) {
      if(!copy($tmp_path,$path_of_uploaded_file))  {
        echo  '\n error while copying the uploaded file';
      }
    }


I don´t think you can upload files like that using jquery. However, it seems you can use the jQuery Form Plugin to upload files but I haven´t tried it yet.


Make sure that cv folder is in your root directory:

$name_of_uploaded_file =  basename($_FILES['uploadcv']['name']);
$type_of_uploaded_file = 
substr($name_of_uploaded_file, 
strrpos($name_of_uploaded_file, '.') + 1);

$size_of_uploaded_file = $_FILES["uploadcv"]["size"]/1024;//size in KBs

$max_allowed_file_size = 100; // size in KB 
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", 'doc', 'docx', 'pdf');
**$upload_folder = './cv/';** //<-- this folder must be writeable by the script
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
$tmp_path = $_FILES["uploadcv"]["tmp_name"];
if(is_uploaded_file($tmp_path)) {
  if(!copy($tmp_path,$path_of_uploaded_file))  {
    echo  '\n error while copying the uploaded file';
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜