0

I am making an application on my server, where the user uploads an image through some HTML combined with javascript.

The user finds an image on the computer through

<form action="uploadimage.php" method="post"
enctype="multipart/form-data">
<label for="file">Filnavn:</label>
<input type="file" name="file" id="file" value="100000" />

Then the point behind the javascript, is to validate on the users image

if(picture_headline.value == "" || picture_uploaded.value == "" || !ischecked)
{
   // Don't execute, stay on same site
}
else
{
   // execute php and upload image
}

the php is an upload image php script

<?php 
// The file is being uploaded into the folder "upload"
$target = "/navnesutten.eu/facebook/uploads/"; 
// add the original filename of our target path
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 
// Moves the uploaded file into correct folder
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} 
else {
echo "Sorry, there was a problem uploading your file.";
}
?> 

I must say I am a bit confused here, since I have only been working with html, php and javascript for a few days now.

Am I totally off or what?

I found some "simple" examples online, which I put on my server through cuteFTP, but everytime i press upload, the website just sends me to the .php file and says the site doesn't exist.

4
  • 9
    Java != Javascript. Commented Aug 27, 2012 at 13:49
  • Do you have a PHP server set up and running? Commented Aug 27, 2012 at 13:49
  • You'll need to use AJAX for what you want. Commented Aug 27, 2012 at 13:50
  • There's a categorical difference between Java and JavaScript. Are you sure uploadimage.php exists in the same path as the page with the form? Commented Aug 27, 2012 at 13:51

2 Answers 2

1

Like Boann points out you're trying to access a non-existent file in your PHP code ("uploaded" and "uploadedfile" rather than "file" (which is what you named the field in your HTML form)).

But regarding "running PHP from JavaScript": You don't have to. The JavaScript should only return false if the form is invalid. If it's valid you don't need to do anything and the form will submit, in turn running your PHP script:

form.onsubmit = function () {
    if (!formIsValid()) {
        return false;
    }
};

If the form is invalid it won't submit (the return false bit (you could use event.preventDefault() instead)), if it is valid nothing will happen and the form will do what it does (ie submit the data to the server).

Sign up to request clarification or add additional context in comments.

2 Comments

The code you wrote. Is it something I will need in my code? My code ends with the if else. if will return false else will return true Will this really execute the upload_file.php ? Don't I need some code somewhere to make that happen ?
As long as your form points to your script (action="upload_file.php") the form data will be sent to your script unless the JS returns false. Which it should only do when the form is invalid. When the form is valid (your else) nothing needs to be done from JS. The form just needs to submit normally. You should definitely check in your upload_file.php for form invalidity as well since people can just disable JS.
0

Each array key in $_FILES corresponds with the name attribute of a file field in the form, so to match your form it should be 'file' rather than 'uploaded' or 'uploadedfile':

<?php 
// The file is being uploaded into the folder "upload"
$target = "/navnesutten.eu/facebook/uploads/"; 
// add the original filename of our target path
$target = $target . basename( $_FILES['file']['name'] ) ; 
$ok=1; 
// Moves the uploaded file into correct folder
if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
} 
else {
echo "Sorry, there was a problem uploading your file.";
}

Comments

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.