Jump to content

Archived

This topic is now archived and is closed to further replies.

lpxxfaintxx

Tricky PHP Dead Ends..

Recommended Posts

[code] <?php
ob_start();
include 'db.php';
    $description = $_POST['description'];
    $description = $_POST['category'];
    $username = $userdata['user_name'];
    $filename = str_replace(' ', '', $_FILES['userfile']['name']);
    $tblw = strlen($filename);
    $ext = substr($filename, $tblw-3, $tblw);
    $exts = array("png", "gif", "jpg");
if ($ext == 'png' OR $ext == 'gif' OR $ext == 'jpg') {
    $idq = mysql_query("SELECT `id` FROM `files` ORDER BY `id` DESC LIMIT 1");
    $ida = mysql_fetch_assoc($idq);
    $id = $ida['id'] + 1; im
    $uploaddir = '/wamp/www/aimphotogallery/upload/$owner';
    $path = '/wamp/www/aimphotogallery/upload/$owner'.$id. '.'.$ext;
    $uploadfile = $uploaddir . $id . "." . $ext;
    $idpath = 'http://localhost/aimphotogallery/upload/'.$id. '.'.$ext;
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
mysql_query("INSERT INTO `memberuploads` (`id`,`path`,`owner`,`idpath`,`description`) VALUES('$id','$path','$username','$idpath','$description')");
mysql_query("INSERT INTO `files` (`id`,`path`,`owner`,`idpath`,`description`) VALUES('$id','$path','$username','$idpath','$description')");
header("Location: http://localhost/aimphotogallery/viewimage.php?id=$id");
};
ob_end_flush();
?>[/code]

1) I am trying to make an error message when something has gone wrong while uploading. The line [code]if ($ext == 'png' OR $ext == 'gif' OR $ext == 'jpg') {[/code] makes it harder to do. I'm really confused on how I should do it.

2) This is my general upload script. How would I integrate Javascript so that users can upload multiple files? The oddness of how this script works makes everything a bit harder :(

3) [code]    $uploaddir = '/wamp/www/aimphotogallery/upload/$owner';[/code]
If the directory doesn't exists, how can I create the directory within this script?

Links, suggestions, answers welcome!

Regards,
LPXXFAINTXX

Share this post


Link to post
Share on other sites
you may have to do something like this

[code]
if ($ext == 'png' OR $ext == 'gif' OR $ext == 'jpg') {
your upload code here
}
else{
echo "Sorry you are trying to upload an unsupported format";
}
[/code]

and for the upload directory to create a new dir


[code]

mkdir("path/to/new/dir/$owner") or die("Sorry I could not make the directory you requested<BR>");


[/code]

[example: mkdir("c:/inetpub/wwwroot/$owner") ]

Share this post


Link to post
Share on other sites
[!--quoteo(post=356342:date=Mar 19 2006, 12:44 AM:name=shortj75)--][div class=\'quotetop\']QUOTE(shortj75 @ Mar 19 2006, 12:44 AM) [snapback]356342[/snapback][/div][div class=\'quotemain\'][!--quotec--]


mkdir("path/to/new/dir/$owner") or die("Sorry I could not make the directory you requested<BR>");
[/code]

[example: mkdir("c:/inetpub/wwwroot/$owner") ]
[/quote]

What if the directory exists? Would the script run properly?

Share this post


Link to post
Share on other sites
I cleaned it up a bit, maybe you can glean some useful info from my code.

[code]<?php
ob_start();
include 'db.php';

if (!isset($_FILES['userfile'])) throw_exception("You didn't upload a file.");

// which of these is correct? they can't both be
$description = $_POST['description'];
$description = $_POST['category'];

$username = $userdata['user_name'];

$filename = str_replace(' ', '', $_FILES['userfile']['name']);
$tblw = strlen($filename);
$ext = substr($filename, $tblw-3, $tblw);
$exts = array("png", "gif", "jpg");

if (!in_array($ext, $exts)) throw_exception("Unsupported file extension.");

$idq = mysql_query("SELECT MAX(id) FROM files") or throw_exception(mysql_error());
$ida = mysql_fetch_assoc($idq);
$id = $ida['id'] + 1;

$dir = '/wamp/www/aimphotogallery/upload';
if (!file_exists($dir)) mkdir($dir, 0777) or throw_exception("Could not create upload directory");

$path = $dir . '/'.$owner.$id.'.'.$ext;
$idpath = 'http://localhost/aimphotogallery/upload/'.$id. '.'.$ext;

move_uploaded_file($_FILES['userfile']['tmp_name'], $path) or throw_exception("Could not complete upload.");

mysql_query("INSERT INTO `memberuploads` (`id`,`path`,`owner`,`idpath`,`description`) VALUES('$id','$path','$username','$idpath','$description')")
    or throw_exception(mysql_error());
mysql_query("INSERT INTO `files` (`id`,`path`,`owner`,`idpath`,`description`) VALUES ('$id','$path','$username','$idpath','$description')")
    or throw_exception(mysql_error());

header("Location: http://localhost/aimphotogallery/viewimage.php?id=$id");

function throw_exception($msg) {
    echo $msg;
    exit;
}
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.