/    Sign up×
Community /Pin to ProfileBookmark

Can’t upload pictures and files by php using Xampp

Hi everyone , i really need your help on uploading pictures and files,im using Xampp for my tests and i have the following code .
—————–


[code=php]
<html>
<head>
<meta http-equiv=”Content-Type”content=”text/html; charset=utf-8″ />
<title>Upload an Image</title>
<style type=”text/css” title=”text/css”media=”all”>
.error {
font-weight: bold;
color: #C00;
}
</style>
</head>
<body>

<?php #script upload_image.php

//check if the form has been submitted
if($_SERVER[‘REQUEST_METHOD’] == ‘POST’){

// check for an uploaded file:
if (isset($_FIlES[‘upload’])){

//Validate the type. should be JPEG or PNG
$allowed = array (‘image/pjpeg’,
‘image/jpeg’, ‘image/JPG’, ‘image/
X-PNG’, ‘image/PNG’, ‘image/png’,
‘image/x-png’);
if(in_array($_FILES[‘upload’][‘type’],$allowed)){

//Move the file over.
if (move_uploaded_file($_FILES[‘upload’][‘tmp_name’],
“/uploads/{$_FILES[‘upload’][‘name’]}”)){
echo ‘<p><em> The file has been uploaded!</em></p>’;
} //End of Move…. IF
}else { //Invalid Type.
echo ‘<p Class=”error”> Please upload a jpeg or Png image.</p>’;
}

} //End of Isset ($_FILES[”upload]) IF.

//Check for errors;
if($_FILES[‘upload’][‘error’] > 0){
echo ‘<p class=”error”>The file could
not be uploaded because: <strong>’;
// Print a message based upon the error
switch($_FILES[‘upload’][‘error’]){
case 1:
print ‘The file exceeds the upload_max_filesize setting
inphp.ini.’;
break;
case 2:
print ‘The file exceeds the MAX_FILE_SIZE setting in
the HTML form.’;
break;
case 3:
print ‘The file was only partially uploaded.’;
break;
case 4:
print ‘No file was uploaded’;
break;
case 6:
print ‘No temporary folder was available’;
break;
case 7:
print ‘Unable to write to the disk’;
break;
case 8:
print ‘file upload stopped.’;
break;
default:
print ‘A system error occurred.’;
break;
}// End of switch
print ‘</strong></p>’;
} // End of Error if

//Delete the file if it still exists:
if (file_exists ($_FILES[‘upload’][‘tmp_name’])
&& is_file($_FILES[‘upload’][‘tmp_name’]) ) {
unlink ($_FILES[‘upload’][‘tmp_name’]);
}
} // End of submitted conditional. ($_SERVER)
?>

<form enctype=”multipart/form-data” action=”upload_image.php” method=”POST”>

<input type=”hidden” name=”MAX_FILE_SIZE” value=”524288″ />
<fieldset><legend> Select a JPEG or PNG image of 512KB or smaller to be uploaded: </legend>
<p><b>File:</b><input type=”file” name=”upload” /></p>
</fieldset>
<div align=”center”><input type=”submit” name=”submit” value=”submit” />
</div>
</form>
</body>
</html>[/code]


————————————

When i test it in my browser ,and i select a picture from my computer and try to upload it , it return the same page with no error or any messages , when i go to “/localhost/uploads/ ” i can’t see any successful uploads, note that the folder of “uploads” is defined as writable and has all permission to apply the test to it . also php.ini is configured correctly .MAX_FILE_SIZE is correct 2MB , file_uploads is on , everythings seems to be in order, why can’t i upload files successfully ? please help me out guys

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@TrainApr 16.2016 — Windows local uses

Internet uses /
Copy linkTweet thisAlerts:
@A-CUBEauthorApr 16.2016 — It didn't work the following message appeared

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:xampphtdocsupload_image.php on line 32
Copy linkTweet thisAlerts:
@NogDogApr 16.2016 — It didn't work the following message appeared

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:xampphtdocsupload_image.php on line 32[/QUOTE]


You need to show us the edited code and a few lines around it (preferably with [noparse][code=php]...[/code][/noparse] tags around it this time), letting us know which line is #32.
Copy linkTweet thisAlerts:
@A-CUBEauthorApr 23.2016 — Line 32 is where the link is not working "/localhost/uploads/" .
Copy linkTweet thisAlerts:
@ginerjmApr 23.2016 — You need to clip and paste some code in its entirety so we can see what you are seeing. You're probably missing a semi or a quote above that line.
Copy linkTweet thisAlerts:
@A-CUBEauthorApr 24.2016 — Hi Ginerjm , as you see below , i highlighted line 32 , i will apply your error code details in my php code , but kindly note that the code below is the full written code.

Its really driving me crazy , i will try to make it work.



<html>

<head>

<meta http-equiv="Content-Type"content="text/html; charset=utf-8" />

<title>Upload an Image</title>

<style type="text/css" title="text/css"media="all">

.error {

font-weight: bold;

color: #C00;

}

</style>

</head>

<body>

<?php #script upload_image.php

//check if the form has been submitted

if($_SERVER['REQUEST_METHOD'] == 'POST'){

// check for an uploaded file:

if (isset($_FIlES['upload'])){

//Validate the type. should be JPEG or PNG
$allowed = array ('image/pjpeg',

'image/jpeg', 'image/JPG', 'image/

X-PNG', 'image/PNG', 'image/png',

'image/x-png');

if(in_array($_FILES['upload']['type'],$allowed)){

//Move the file over.
if (move_uploaded_file($_FILES['upload']['tmp_name'],
[B]"../uploads/{$_FILES['upload']['name']}"[/B])){
echo '<p><em> The file has been uploaded!</em></p>';
} //End of Move.... IF
}else { //Invalid Type.
echo '<p Class="error"> Please upload a jpeg or Png image.</p>';
}


} //End of Isset ($_FILES[''upload]) IF.

//Check for errors;

if($_FILES['upload']['error'] > 0){

echo '<p class="error">The file could

not be uploaded because: <strong>';

// Print a message based upon the error

switch($_
FILES['upload']['error']){

case 1:

print 'The file exceeds the upload_max_filesize setting

inphp.ini.';

break;

case 2:

print 'The file exceeds the MAX_FILE_SIZE setting in

the HTML form.';


break;

case 3:

print 'The file was only partially uploaded.';


break;

case 4:

print 'No file was uploaded';


break;

case 6:

print 'No temporary folder was available';


break;

case 7:

print 'Unable to write to the disk';


break;

case 8:

print 'file upload stopped.';


break;

default:

print 'A system error occurred.';

break;

}// End of switch

print '</strong></p>';


} // End of Error if

//Delete the file if it still exists:

if (file_exists ($_FILES['upload']['tmp_name'])

&& is_file($_
FILES['upload']['tmp_name']) ) {

unlink ($_FILES['upload']['tmp_name']);

}

} // End of submitted conditional. ($_
SERVER)

?>

<form enctype="multipart/form-data" action="upload_image.php" method="POST">

<input type="hidden" name="MAX_FILE_SIZE" value="524288" />

<fieldset><legend> Select a JPEG or PNG image of 512KB or smaller to be uploaded: </legend>

<p><b>File:</b><input type="file" name="upload" /></p>

</fieldset>

<div align="center"><input type="submit" name="submit" value="submit" />

</div>

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@ZorgMay 04.2016 — If this is a copy and paste then you have a typo its [I]$_FILES [/I]not [I]$_FIlES[/I].

[B]Change:[/B]

[code=php]if (isset($_FIlES['upload'])){[/code]

[B]should be:[/B]

[code=php]if (isset($_FILES['upload'])){[/code]

to help debug $_FILES, place this at the top before all conditionals and make sure all your if statements are working.

[code=php]
echo '<pre>';
var_dump($_FILES['upload']);
echo '</pre>;
[/code]
Copy linkTweet thisAlerts:
@A-CUBEauthorMay 04.2016 — Oh My god , i didn't realize that did this mistake , i can't barely see it , thanks for the tip Zorg , i will try that shortly . Thanks again Bud .
Copy linkTweet thisAlerts:
@rootMay 04.2016 — You should always let the server calculate the path, especially those that rely on the file system.

Suggest you read the image upload how to in the sticky post of this forum, its very helpful to writing your own.
Copy linkTweet thisAlerts:
@alexauMay 04.2016 — you made a mistake in spelling
Copy linkTweet thisAlerts:
@A-CUBEauthorMay 04.2016 — Thanks guys , im still at learning stage , but at least i can design some simply website pages now .Thanks all
×

Success!

Help @A-CUBE spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...