Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7635689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:29:22+00:00 2026-05-31T07:29:22+00:00

I’m using this php code for an image upload. But I keep getting an

  • 0

I’m using this php code for an image upload. But I keep getting an error towards the end. The result “Error while uploading image. Please Try Again. I feel like I have checked everything but the dang image just wont copy into the uimages folder. Please help…

Here is the code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php 

function create_thumbnail($source,$destination,$thumb_width){

    $size = getimagesize($source);
    $width = $size[0];
    $height = $size[1];
    $x = 0;
    $y = 0;

    if($width > $height){

        $x = ceil(($width - $height) / 2);
        $width = $height;
        }
            else if($height> $width){
            $y =(($height - $width) /2);
            $height = $width;
}

$new_image = imagecreatetruecolor($thumb_width,$thumb_height) or die('Cannot Initiatlize new GD image stream');

$extension = get_image_extension($source);
if($extension=='jpg' || $extension=='jpeg')
    $image= imagecreatefromjpeg($source);
if($extension=='gif')
    $image= imagecreatefromgif($source);
if($extension=='png')
    $image= imagecreatefrompng($source);


imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
if($extension=='jpg' || $extension=='jpeg')
    imagejpeg($new_image,$destination);
if($extension=='gif')
    imagegif($new_image,$destination);
if($extension=='png')
    imagepng($new_image,$destination);

}

function get_image_extension($name){
    $name=strtolower($name);
    $i = strrpos($name,".");
    if(!$i) {return "";}
        $l = strlen($name) - $i;
        $extension = substr($name,$i+1,$l);
        return $extension;  
    }

function random_name($length){
    $characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $name = "";

    for($i = 0; $i < $length; $i++){
        $name .= $characters[mt_rand(0,strlen($characters) - 1)];

        }
        return "image-".$name;

    }   

    $images_location = "images";
    $thumbs_location ="uimages/thumbs";
    $thumb_width = 100;
    $maximum_size = 5120;

    $results = "Click to browse to locate the image you want to upload.";

    if($_POST){
        if($_FILES['image']['name'] == "")
{
    $results="No Image Selected. Click Browse to find an image";

    }   
    else{
        $size=filesize($_FILES['image']['tmp_name']);
        $filename = stripslashes($_FILES['image']['name']);
        $extension = get_image_extension($filename);
        if($size> $maximum_size){
            $results="Your file exceeds the maximum size.";

            }
        else
        if(($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){
            $results="Invalid image type. Must be jpg, jpeg, png, or a gif file.";
            }
            else{
                $image_random_name = random_name(15).".".$extension;
    $copy= @copy($_FILES['image']['tmp_name'], $images_location.$image_random_name);

if(!$copy){
    $results = "Error while uploading image. Please try again.";}
    else{
        create_thumbnail($images_location.$image_random_name,$thumbs_location.$image_random_name,$thumb_width);
        $results="Image has been uploaded";

        }

    }


                }
        }   



?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="5120" >
<input type="file" name="image" />
<input type="submit" value="Upload Image" />
</form>

<?php echo $results; ?>

</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:29:23+00:00Added an answer on May 31, 2026 at 7:29 am

    Try moving the file like this:

    if (move_uploaded_file($_FILES['image']['tmp_name'], $images_location.$image_random_name))
    {
        //success
    }
    else
    {
        //fail
    }
    

    Also check that the target location exists and is being set correctly, and that permissions of the target location are correct- Ensure that apache (or whatever is running) has the correct write access. To set the permissions for apache it is something like this:

    chown -R nobody directory_name
    chmod 755 -R directory_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.