/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Best approaches/patterns to do this?

Something so common as is having a form in which one or some fields are of type “file”, so that when the user sends the form (the user would be an admin in this case because this would be implemented in the backend), the process takes care of storing the data in the DB (including the names of the files), uploading the files with the ftp functions to their destination, and everything is controlled so that when there is an error, the process stops and reverts any change, to prevent having a residual record in the database with no existing file, or a file with no record pointing to it. Of course form validation and filtering (PHP + javascript) would also be included.

What’s a good approach, or a good design pattern, to implement this?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 01.2011 — I suppose I'd just make sure each step in the sequence succeeded, and only proceed to the DB update once I knew I had a good file upload. Then I suppose you'd need a step to clean up the file system if the DB update failed for some reason, e.g.:
[code=php]
$errors = array();
if(!empty($_FILES['upload']['size'])) {
$ext = pathinfo($_FILES['upload']['file_name'], PATHINFO_EXTENSION);
$fileName = uniqid();
if(!empty($ext)) {
$fileName .= ".$ext";
}
if(move_uploaded_file($_FILES['tmp_name'], 'upload/dir/' . $fileName)) {
$sql = "whatever you want to insert into DB";
$result = mysql_query($sql);
if($result == false or mysql_affected_rows() == 0) {
unlink 'upload/dir/' . $fileName;
$errors[] = "Unable to update database, please try again.";
// maybe write some debug info to error_log() here?
}
}
else {
$errors[] = "Unable to save uploaded file, please try again.";
}
}
else {
$errors[] = "No file uploaded or it was an empty file";
}
if(!empty($errors)) {
foreach($errors as $err) {
echo "<p class='error'>".htmlspecialchars($err)."</p>n";
}
}
[/code]
Copy linkTweet thisAlerts:
@JazztronikauthorAug 02.2011 — That's a good, fast and efficient way. I would have made something similar, as I actually do in projects like this, with slight differences like using a string with line breaks for the errors instead of an array, and ftp functions because we use to store the files in a different place.

But I was asking for something more scalable, using objects, and their relationships. I was wondering if is there any common design pattern to do this.
Copy linkTweet thisAlerts:
@NogDogAug 02.2011 — From an OOP aspect, I guess I'd probably have a controller object that would implement the logic in the first reply, then a couple models -- one for managing the file and one for the database info -- that the controller would instantiate internally. Each model could have an undo() method that would delete/undo things and which the controller would call should it determine things have gone pear-shaped at some point.
Copy linkTweet thisAlerts:
@JazztronikauthorAug 05.2011 — Thanks for your help! ?
×

Success!

Help @Jazztronik 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.18,
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,
)...