Jump to content

Tricky PHP Dead Ends..


lpxxfaintxx

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
Link to comment
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") ]
Link to comment
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?
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.