Jump to content

[PHP] image upload script don't works


Mlaaa
Go to solution Solved by Jacques1,

Recommended Posts

Hi

 

As title say i have problem with image uploading. I don't get any error but image don't wanna to upload. Folder have permision 755.

Here is a script :

error_reporting(E_ALL);
include 'header.php';

echo '<h2>Add image</h2>';

$target_dir = '../images/';

// Check if image file is a actual image or fake image
if(isset($_POST['submit']))
{
    
    if(!empty($_FILE['fileToUpload']))
    {
            $target_file = $target_dir . basename($_FILES['fileToUpload']['name']);
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            $check = getimagesize($_FILES['fileToUpload']['tmp_name']);
            if($check !== false) {
                // Check if file already exists
                if (file_exists($target_file))
                {
                    $error = '<p class="fail">Sorry, file already exists.</p>';
                }
                else
                {
                        if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)) {
                                $error = '<p class="success">The file '. basename( $_FILES['fileToUpload']['name']). ' has been uploaded.</p>';
                        }
                        else
                        {
                                $error = '<p class="fail">Sorry, there was an error uploading your file.</p>';
                        }
                }
            }
            else
            {
                $error = '<p class="fail">File is not an image.</p>';
            }
    }
    else
    {
        $error = '<p class="fail">Please select image.</p>';
    }    
}

And form html :

<?php if(!empty($error)) { echo $error; } ?>
<form action="" method="post" enctype="multipart/form-data">
	<fieldset>
		<legend></legend>
    		<p>
                <label for="file">Filename : </label> 
    		<input type="file" name="fileToUpload" id="file">
                </p>
    		<p><input type="submit" name="submit" value="Save"></p>
    </fieldset>
</form>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.