1

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.replace(/^\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???

4
  • 3
    can you post Validare() too? It should return true to let your form submit. Commented Dec 14, 2009 at 20:27
  • the Validare() return true. I've tested it. Commented Dec 14, 2009 at 20:33
  • $tmp_name = $_FILES["file"]["tmp_name"][$i]; This is from the for loop. Commented Dec 14, 2009 at 20:36
  • Please show us the code for Validare() Commented Dec 14, 2009 at 20:42

7 Answers 7

2

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

And is the target directory "is_writeable" ? Together with all the necessary unix file permission ? For example, the directory could be chmoded to 0775 "rwxrwxr-x" with a group set to "www" or "apache" (or whatever the PHP process is running as).
1

$tmp_name is never initialized to anything.

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

3 Comments

@Lucas Which part are you referring to?
It was a comment that I had removed.
@Peter Lindqvist: Yes, it's one of my biggest complaints with the "not marked as edited if done within 5 minutes" thing.
0

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

1 Comment

I've try even with "http : // url / fisiere /"; . $name, but nothing.
0

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

3 Comments

You still need the index at the end of $_FILES['file']['tmp_name'] so that the correct file is referenced.
I need this onsubmit="return Validare();" for validation.
this is true and valid in most cases, i agree :)
0

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.

Comments

-1

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

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

1 Comment

Do not use time(). He is using the rand() function to try and get a pseudo random number. If you use time(), you will not get this as multiple files can be uploaded in a given second.
-1

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

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.