dongwang6837 2013-03-30 11:09
浏览 48
已采纳

如果选中无线电,请避免使用$ _FILES帖子

First of all: i'm sorry for my english, i'll try to explain what i want but i'm sorry if i can't do it very well.

I'm trying to create a webpage to allow users to upload images to server, the page have url upload, and file upload, with radio button to select upload method.

My problem is that i don't know how to disable file upload when url radio is selected, for example:

url upload: the webpage read url and use that image. file upload: the webpage read file and use that image.

That's what i want, but the problem is when both is selected: the page upload the file and read url, but only if url radio is selected. If file radio is selected then url is ignored.

I know that the problem is that when you press submit button the file is uploaded, and then the php server check the selected radio button and what have to do.

Is there any mode to avoid file upload if url radio is selected?, here's the script of page:

    <?php
    if ($_POST) {

        if ($_POST["filesource"] === "file"){
            $fileread = $_FILES["file"]["tmp_name"];
        }else{
            $fileread = $_POST["url"];
        }

        if ($fileread){
                include('includes/imgresize.php');
                $image = new SimpleImage();
                $image->load($fileread);
                $image->resize(250,400);
                $image->save('picture.jpg');
                echo "La imagen subida es: <br>";
                echo "<img src='picture.jpg'>";
                echo die;
        }else{
            echo "No se ha seleccionado ningún archivo.";
            echo die;
        }
    }
?>
<script>
    function setrad(radbut){
        document.getElementById(radbut).checked = true;
    }
</script>

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
    <table>
        <tr><td><input type="radio" name="filesource" value="url" id="urlrad" checked>URL:</td><td><input type="text" size="64" name="url" onClick="setrad('urlrad');"></td></tr>
        <tr><td><input type="radio" name="filesource" value="file" id="filerad">File:</td><td><input type="file" size="50" name="file" id="file" onClick="setrad('filerad');"/></td></tr>
    </table>


    <input name="Submit" type="submit" id="submit" value="Submit" />
</form>

Thanks in advance :)

  • 写回答

1条回答 默认 最新

  • dtv8189 2013-03-30 11:23
    关注

    I would suggest you a client-side check with javascript: you could simply clear the right input whenever a radio button is toggled.

    function setrad(radbut, input){
        document.getElementById(radbut).checked = true;
        document.getElementById(input).value = "";
    }
    

    This way you can call your setrad function like onClick="setrad('urlrad','file');" (note that both inputs and radios should have an id).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要