将内容从textarea上传到文件夹

时间:2017-05-31 22:02:53

标签: javascript php html

我目前正在编写一个代码,将textarea的内容作为.html文件上传到名为uploads的文件夹中。我想在用户按下共享时运行php脚本。

<a id="wshare" class="btn">Share</a>

<textarea id="icontent" placeholder="Enter your content here." name="mas" rows="15" class="content"></textarea>

<?php
error_reporting(0);
if(isset($_FILES['image'])){
  $errors= array();
  $file_name = rand(5, 1000000);
  $file_size =$_FILES['image']['size'];
  $file_tmp =$_FILES['image']['tmp_name'];
  $file_type=$_FILES['image']['type'];
  $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

  $expensions= array("html");
  if(in_array($file_ext,$expensions)=== false){
    $errors[]="Only Html Files can be uploaded!";
  }
  if($file_size > 2097152){
  $errors[]='Sorry,Looks Like Your File Is Larger Than 2MB =(';
  }
  if(empty($errors)==true){
    move_uploaded_file($file_tmp,"uploads/".$file_name);
    echo "Success-Link: uploads/" .$file_name;
  }else{
    print_r($errors);
  }
}
?>

1 个答案:

答案 0 :(得分:1)

我猜你知道php和javascript不是同时执行的,不是同一个“地方”,也不能一起使用,因为你似乎想要的。

IMO你应该在表单提交时询问用户文件名,用答案填充隐藏的输入字段,然后在php中结合两个值(textarea + hidden input field)

相关问题