/    Sign up×
Community /Pin to ProfileBookmark

upload photo validation

Hello everyone,

I’m quite new in php, and I admit I still have a lot to learn. With this, I’m practicing my php learning by building my website. I have this idea in mind that I would like to upload a picture. I already did the upload script and it does upload. But I’m having a problem in validation. I want to validate if the user did upload a picture. Jpeg, Jpg, and PNG is the only available file types and the size.

How can I make this. I wish I knew how to create my preview.php. Here is the snippet of my code in the user account panel. This is where the uploading happens and then redirects my uploaded file into preview.php. Any suggestions?

[code=php]<html>
<head>
<script type=”text/javascript” src=”mootools-core.js”></script>
<script type=”text/javascript”>

function stopUpload(success, name){
window.addEvent(‘domready’,function(){
$(‘photo_img’).setProperty(‘src’,”members_photo/”+name);
var photo_send = new Request({
method : ‘post’,
url : ‘update_photo.php’,
onSuccess : function(info){
if(info==”updated”){
alert(“Photo updated”);
}
}
});

var split_name = $(‘photo_img’).getProperty(‘src’).split(“/”);
var new_name = split_name[split_name.length-1];
photo_send.send(“img_src=”+new_name);
});
return true;
}

window.addEvent(‘domready’,function(){
var photo_ind = 0;

$(‘photobtn’).addEvent(‘click’,function(){
if(photo_ind == 0){
$(‘photo_upload’).setStyle(‘display’,’block’);
this.set(‘text’,’Hide Upload’);
photo_ind = 1;
}else {
$(‘photo_upload’).setStyle(‘display’,’none’);
this.set(‘html’,’Change Photo’);
photo_ind = 0;
}
});

$(‘photo_img’).setProperty(‘src’,”<?php echo $_SESSION[‘photo’]; ?>”);
});
</head>
<body>
<span id=”photobtn”>Change My Photo</span>
<div id=”photo_upload” class=”upload_form”>
<form id=”photo_form” target=”up_target” enctype=”multipart/form-data” method=”post” action=”preview.php”>
<input type=”file” name=”upload_file” id=”upload_file” />
<button type=”submit” id=”submit_btn”>Upload Photo</button>
</form>
<iframe name=”up_target” id=”up_target” style=”height:0px; width:0px; visibility:hidden; display:none; “></iframe>
</div>
</body>
</html>[/code]

This script is working. It uploads and display the photo the way I want. I wanted to add a validation, which will be performed on preview.php but I’m having a trouble on how to start it with because I’m new in input type=”file” topics. Thanks reply is much appreciated.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperDec 10.2009 — First off, client side validation is fine, but honestly you should validate the upload server side. When any file is uploaded via PHP, one of the $_FILE keys is the mime type, assuming the browser supplies it and in most cases it does. That's what you want to check.

In addition to any other server side validation you perform, here is a quick down and dirty example of what I mean:

[code=php]$permitted = array('image/jpeg','image/pjpeg'); // set allowed MIME types in this array
$typeOK=false; // just setting up the variable with a default value

// Loop through the permitted array, determine if the uploaded image mime type is in the array or not
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type']) {
$typeOK = true;
break;
}
}

// $typeOK will be true if mime type permitted, false if not
// Use this value in your validation code
[/code]


Get the idea?

-jim
×

Success!

Help @kingdm 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 5.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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...