我目前正在编写一个代码,该代码会将文本区域的内容作为 .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);
}
}
?>
最佳答案
我猜你知道 php 和 javascript 不是同时执行的,不是在同一个“地方”,并且不能一起使用,因为你似乎有意这样做。
IMO,您应该在提交表单时向用户询问文件名,用答案填充隐藏的输入字段,然后在 php 中组合这两个值(文本区域 + 隐藏的输入字段)
关于javascript - 将文本区域中的内容上传到文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44295365/