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 8227581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:10:23+00:00 2026-06-07T16:10:23+00:00

I am using the following code to upload a file in PHP for my

  • 0

I am using the following code to upload a file in PHP for my website. I’ll post the code first and then ask my question.

upload.php:

<html>
<form action="upload_process.php" method="post" enctype="multipart/form-data">
<label for="file">Select picture to upload:</label>
<input type="file" name="image" id="image" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</html>

upload_process.php

<?php
function error ($error, $location, $seconds = 1)
{
    header ("Refresh: $seconds; URL = \"$location\"");
    echo $error;
    exit;
}
$max_file_size = 1000000;
$current_directory = str_replace(basename($_SERVER['PHP_SELF']) , '' , $_SERVER['PHP_SELF']);
$uploaded_directory = $_SERVER ['DOCUMENT_ROOT'] . $current_directory . 'files/';
$upload_form = 'http://' . $_SERVER['HTTP_HOST'] . $current_directory . 'upload.php';
$upload_success = 'http://' . $_SERVER['HTTP_HOST'] . $current_directory . 'view_posters.php';
$fieldname = 'image';
$errors = array (1 => "Max file size exceeded", 2 => "Max file size exceeded", 3 => "Upload incomplete", 4 => "No file attached");
isset ($_POST['submit']) or error ("Redirecting to upload form", $upload_form);
($_FILES[$fieldname]['error'] == 0) or error ($errors[$_FILES[$fieldname]['error']], $upload_form);
@is_uploaded_file($_FILES[$fieldname]['tmp_name']) or error ("Not an HTTP upload", $upload_form);
@getimagesize($_FILES[$fieldname]['tmp_name']) or error ("Please upload only images", $upload_form);
if (file_exists($upload_filename = $uploaded_directory . $_FILES[$fieldname]['name']))
{
    error ("File exists", $upload_form);
}
$uploadedfile = $_FILES[$fieldname]['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newwidth = 500;
$newheight = 500;
$tmp=imagecreatetruecolor ($newwidth, $newheight);
imagecopyresampled ($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg ($tmp, "files/" . $_FILES[$fieldname]['name'], 100);
imagedestroy($src);
imagedestroy($tmp);
header ("Location: " . $upload_success);
?>

Now, for my question, if the file name doesn’t have a space, it gets resized and uploaded correctly. But, if there is a space, weird things happen. For example, if the name of the file is “this image.jpg” after upload, it gets resized and saved onto the server as “this.jpg image.jpg”. Can you please tell me why this is happening.

And, $upload_success is a landing page which just says “Upload successful”.

  • 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-06-07T16:10:24+00:00Added an answer on June 7, 2026 at 4:10 pm

    Well presumably you wouldn’t want the files saving with spaces anyway. So you should be checking and correcting this as part of the upload validation.

    imagejpeg($tmp, "files/".str_replace(' ', '_', $_FILES[$fieldname]['name']), 100)
    

    That will replace spaces with underscores.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am initializing file upload using the following HTML: <form enctype=multipart/form-data action=PHPScripts/upload.php method=POST> <input
I am using the following code, upload.php, to try to have someone upload a
I am using the following code: <?php $token = $_REQUEST['token']; $file = $_REQUEST['file']; $sig
I am using the following code to upload the file <asp:UpdatePanel ID=UpdatePanel18 runat=server> <ContentTemplate>
I am trying to upload file to rackspace cloud file using the following code:
I'm using WordPress and I'm following W3's guide for uploading a file: HTML code:
I am using cake PHP file upload with the AJAX form submit. But it
I am trying to upload file using dropbox api but following Code shows some
I have the following form: <form id=vintro-upload-form action={url}?nexturl={nextUrl} method=post enctype=multipart/form-data > <input name=file type=file/>
All, I'm using the following code to upload some images to my Wordpress blog:

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.