开发者

Php upload file

The code looks like this:

html

<form action="contact.php" method="post" enctype="multipart/form-data" onsubmit="return Validare();">

    <input type="text" name="nume" value="Nume" class="contact" id="Nume" onclick="if(this.value=='Nume')this.value='';" onblur="if(this.value.replace(/^\s+|\s+$/g,'')=='')this.value='Nume'" /><font color="red">*</font><br />
            <input type="text" name="email" value="Email" class="contact" id="Email" onclick="if(this.value=='Email')this.value='';" onblur="if(this.value.replace(/^\s+|\s+$/g,'')=='')this.value='Email'" /><font color="red">*</font><br />
            <input type="text" name="telefon" value="Telefon" class="contact" id="Telefon" onclick="if(this.value=='Telefon')this.value='';" onblur="if(this.value.replace(/^\s+|\s+$/g,'')=='')this.value='Telefon'" /><br />
            <textarea name="mesaj" rows="10" class="contact" id="Mesaj" onclick="if(this.value=='Mesaj')this.value='';" onblur="if(this.value.replace(/^\s+|\s+$/g,'')=='')this.value='Mesaj'">Mesaj</textarea>

<input type="file" name="file[]" />
<input type="file" name="file[]" />
<input type="file" name="file[]" />
<input type="submit" value="Trimite" />
</form>

php

for($i=0; $i<3; $i++){
if($_FILES["file"]["size"][$i] > 0){
    $rand = rand(10000, 99999);
    $name = $rand.rand(10000, 99999).$_FILES["file"]["name"][$i];
            $tmp_name = $_FILES["file"]["tmp_name"][$i];
    $target_path_big = "http://biroutraduceri.net/fisiere/".$name;
    move_uploaded_file($tmp_name, "fisiere/".$name);
}
}

javascript

<script>
function Validare(){
    if(document.getElementById("Nume").value.replace(/^\s+|\s+$/g,'') == "" || document.getElementById("Nume").value.replace(/^\s+|\s+$/g,'') == "Nume"){
        alert("Numele nu este valid!");
        return false;
    }
    if(document.getElementById("Email").value.replac开发者_开发百科e(/^\s+|\s+$/g,'') == "" || document.getElementById("Email").value.replace(/^\s+|\s+$/g,'') == "Email"){
        alert("Email-ul nu este valid!");
        return false;
    }
    if(document.getElementById("Mesaj").value.replace(/^\s+|\s+$/g,'') == "" || document.getElementById("Mesaj").value.replace(/^\s+|\s+$/g,'') == "Mesaj"){
        alert("Mesajul nu este valid!");
        return false;
    }
    return true;
}
</script>

When I press submit nothing happen. The file isn't uploaded.

Where I'm wrong???


Your PHP code has an error, the $tmp_name is never set.

Corrected code

for($i=0; $i<3; $i++){
    if($_FILES["file"]["size"][$i] > 0){
        $rand = rand(10000, 99999);
        $name = $rand.rand(10000, 99999).$_FILES["file"]["name"][$i];
        $target_path_big = "http://biroutraduceri.net/fisiere/".$name;
        move_uploaded_file($_FILES["file"]["tmp_name"][$i], "fisiere/".$name);
    }
}


$tmp_name is never initialized to anything.

$tmp_name should be set equal to $_FILES['file']['tmp_name'][$i];


Could be a permission problem, does your script have permission to write in "fisiere/".$name and is "fisiere/".$name really where you think it is? You might want to use an absolute path.

Edit: You cannot write an image to a http url, you need to write it to a local file-path and you need to make sure php has permissions to write to that path / directory


You realy dont need the onsubmit="return Validare();" and it is even written wrong. and move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) is the correct code for


You forgot to set up the variable $tmp_name. As in $tmp_name = $_FILES["file"]["tmp_name"][$i]; Otherwise, it seems ok as per my own tests.

Otherwise, throw in a print_r($_FILES); before your "for" loop, a couple more prints and a is_readable($tmp_name) check inside your loop, just to more finely try to pin-point the source of the problem.


i guess onsubmit="return Validare();" is returning false

why are you using this rand function anyway, try time() it's better i think


you want $_FILES['file'][$i]['size']

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜